Game Development Reference
In-Depth Information
'Check for transitions
Dim nextStateName As String
nextStateName = stateObj.TransitionCheck(World)
'Did we get one?
If nextStateName <> "" Then
'Make a transition
If States.Contains(nextStateName) Then
'Leave this state
stateObj.ExitFunction(World)
'Switch states
stateObj = States(nextStateName)
currentStateName = nextStateName
'Enter and run the new state
stateObj.Entry(World)
stateObj.Update(World)
Else
World.Say("ERROR: State " & stateObj.GetType.Name & _
" wants to transition to " & nextStateName & _
" but that state is not in the machine!")
End If
Else
'Just run the update of the current state
stateObj.Update(World)
End If
Else
World.Say("ERROR: Current state " & currentStateName & _
" is not found in machine!")
End If
End Sub
This code has two error checks. The first makes sure the current state can be
found by name in the collection of states in the machine. This error protects
against any programming errors involving the name of the current state. This
error is unlikely, but by checking first we keep the code from crashing. We use
the monster's Say function to complain about the problem. Real game code
would have a real error log. The second error check makes sure that any state
called for by a transition is present in the machine. This type of error is far more
likely; forgetting to load a state into the machine is a data error and not an
algorithm or coding error. It will not show up unless a particular transition to
the missing state executes. All the FSM code can be correct, but it needs to be
correctly initialized.
 
Search Nedrilad ::




Custom Search