Game Development Reference
In-Depth Information
Adding states and transitions
As states are contained in a table where the state's name is the look-up key, we simply cre-
ate a new
FiniteState
instance and add it to the table. Adding transitions requires
checking whether each of the transitions to and from states exist within the FSM, and then
inserting the transition within the
transitions_
class variable.
Going forward, as we create states and transitions, we'll represent each state as well as
every possible transition using the following diagram:
A finite state machine
Creating our
AddState
and
AddTransition
functions will modify the internal tables
held by the FSM in order to add additional states and map one state to another using trans-
itions
FiniteStateMachine.lua
:
function FiniteStateMachine.AddState(self, name, action)
self.states_[name] = FiniteState.new(name, action);
end
function FiniteStateMachine.AddTransition(
self, fromStateName, toStateName, evaluator)
-- Ensure both states exist within the FSM.