Class StateMachine<TLabel>
- Namespace
- Gamelogic.Extensions
- Assembly
- Assembly-CSharp.dll
A lightweight state machine.
[Version(1, 0, 0)]
public class StateMachine<TLabel>
Type Parameters
TLabel
The label type of this state machine. Enums are common, but strings or int are other possibilities.
- Inheritance
-
StateMachine<TLabel>
- Derived
- Inherited Members
- Extension Methods
Remarks
To use it:
- Define your own label. Enums are probably the best choice.
- Construct a new state machine, typically in a MonoBehaviour's Start method.
- Add the various states with the appropriate delegates.
- Call the state machine's Update method from the MonoBehaviour's Update method.
- Set the CurrentState property on the state machine to transition. (You can either set it from one of the state delegates, or from anywhere else.
When a state is changed, the OnStop on existing state is called, then the OnStart of the new state, and from there on OnUpdate of the new state each time the update is called.
Constructors
StateMachine()
Constructs a new StateMachine.
public StateMachine()
Properties
CurrentState
Returns the label of the current state.
public TLabel CurrentState { get; set; }
Property Value
- TLabel
Methods
AddState(TLabel)
Adds a state.
public void AddState(TLabel label)
Parameters
label
TLabelThe name of the state to add.
AddState(TLabel, Action)
Adds a state, and the delegates that should run when the state starts.
Any delegate can be null, and wont be executed.
public void AddState(TLabel label, Action onStart)
Parameters
label
TLabelThe name of the state to add.
onStart
ActionThe action performed when the state is entered.
AddState(TLabel, Action, Action)
Adds a state, and the delegates that should run when the state starts, and when the state machine is updated.
Any delegate can be null, and wont be executed.
public void AddState(TLabel label, Action onStart, Action onUpdate)
Parameters
label
TLabelThe name of the state to add.
onStart
ActionThe action performed when the state is entered.
onUpdate
ActionThe action performed when the state machine is updated in the given state.
AddState(TLabel, Action, Action, Action)
Adds a state, and the delegates that should run when the state starts, stops, and when the state machine is updated.
Any delegate can be null, and wont be executed.
public void AddState(TLabel label, Action onStart, Action onUpdate, Action onStop)
Parameters
label
TLabelThe name of the state to add.
onStart
ActionThe action performed when the state is entered.
onUpdate
ActionThe action performed when the state machine is updated in the given state.
onStop
ActionThe action performed when the state machine is left.
AddState<TSubStateLabel>(TLabel, StateMachine<TSubStateLabel>, TSubStateLabel)
Adds a sub state machine for the given state.
The sub state machine need not be updated, as long as this state machine is being updated.
[Version(1, 4, 0)]
public void AddState<TSubStateLabel>(TLabel label, StateMachine<TSubStateLabel> subMachine, TSubStateLabel subMachineStartState)
Parameters
label
TLabelThe name of the state to add.
subMachine
StateMachine<TSubStateLabel>The sub-machine that will run during the given state.
subMachineStartState
TSubStateLabelThe starting state of the sub-machine.
Type Parameters
TSubStateLabel
The type of the sub-machine.
ToString()
Returns the current state name
public override string ToString()
Returns
Update()
This method should be called every frame.
public void Update()