Table of Contents

Class StateMachine<TLabel>

Namespace
Gamelogic.Extensions
Assembly
Gamelogic.Extensions.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.

Properties

CurrentState

Returns the label of the current state.

Methods

AddState(TLabel)

Adds a state.

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.

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.

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.

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.

ToString()

Returns the current state name

Update()

This method should be called every frame.