-
Notifications
You must be signed in to change notification settings - Fork 1
State Machine
A finite state machine is a structure that helps organize the behavior of entities in a game. A state machine behavior can be observed in many devices of our modern world like vending machines. Actions are performed according to a sequence of events and the current state of the machine. The same event, like pressing the same button, may yield a different behavior if the machine is in a different state than before. Think of clocks where time and alarm can be set only by two commands, the short and long press of a button.
The fundamental aspects of the state machine structure implemented in FUDGE are states, actions, transitions, instructions and the state machine itself.
Consider a non-player-character acting as a guard in a game. This guard has a range of action near a gate it observes and a limited set of states it can take on. It reacts to a few events, switches states according to those and performs state-specific actions. It's not very bright, but alive enough to have some fun with it.
state | action | trigger | transition | next state |
---|---|---|---|---|
IDLE | stand around snoozing | timeout | pickup weapon and extend range of perception | PATROL |
see or hear something closeby | prepare weapon and increase speed | CHASE | ||
PATROL | stroll in front of gate | timeout | place weapon and decrease range of perception | IDLE |
see or hear something | set destination and increase speed | CHASE | ||
CHASE | run towards the destination | timeout | decrease speed and set destination to gate | PATROL |
see or hear something elsewhere | CHASE |
The states, actions and transitions are already depicted in the table above. The guard itself is a state machine, either an extension of the class StateMachine or an extension of ComponentStateMachine, which is itself an extension of ComponentScript, that gets attached to a node and control it.
FudgeAid implements a class StateMachineInstructions, which links functions to be executed as actions to a particular state and functions to be executed as transitions to a combination of two states. Given such a set of instructions, a state machine executes these functions depending on its current state or additionally to the state to transition to, when its methods act
or transit
are called. If no function is defined in the set for a particular state or transition, default methods are called that can also be customized.
The class StateMachineInstructions is agnostic to any state machine, so instruction sets could be used by different machines or a machine maybe fed with different instruction sets. The common use case expected though, is that the methods linked are defined as static methods in the state machine. In order to manipulate an instance of that machine, it is passed to the method as a parameter.