Click or drag to resize

StateMachineTLabel Class

A lightweight state machine.
Inheritance Hierarchy
SystemObject
  Gamelogic.ExtensionsStateMachineTLabel
    Gamelogic.ExtensionsPushdownAutomatonTLabel

Namespace:  Gamelogic.Extensions
Assembly:  Assembly-CSharp (in Assembly-CSharp.dll) Version: 0.0.0.0
Syntax
C#
[VersionAttribute(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.

The StateMachineTLabel type exposes the following members.

Constructors
Properties
  NameDescription
Public propertyCurrentState
Returns the label of the current state.
Top
Methods
  NameDescription
Public methodAddState(TLabel)
Adds a state.
Public methodAddState(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 methodAddState(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 methodAddState(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 methodAddStateTSubStateLabel(TLabel, StateMachineTSubStateLabel, 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.
Public methodEquals (Inherited from Object.)
Protected methodFinalize (Inherited from Object.)
Public methodGetHashCode (Inherited from Object.)
Public methodGetType (Inherited from Object.)
Protected methodMemberwiseClone (Inherited from Object.)
Public methodToString
Returns the current state name
(Overrides ObjectToString.)
Public methodUpdate
This method should be called every frame.
Top
Extension Methods
  NameDescription
Public Extension MethodThrowIfNull
Throws a NullReferenceException if the object is null.
(Defined by ObjectExtensions.)
Top
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.

See Also