Game.EndFrameBarrier
Assembly: Assembly-CSharp
Namespace: Game.Rendering.Utilities
Type: class
Base: System.Object
Summary:
StateMachine is a small runtime state machine for managing State instances (a separate State type is expected by the game/mod framework). It holds a single active State, provides lifecycle transitions (TransitionIn/TransitionOut), and exposes Action callbacks for start, stop and transitions. The machine supports Update/LateUpdate-driven progression and will immediately process a transition when one is requested.
Fields
-
public Action onStarted
Callback invoked when the machine is started (after the initial state transition has occurred). -
public Action onStopped
Callback invoked when the machine is stopped (after the current state has been transitioned out). -
public Action<State> onTransitioned
Callback invoked whenever the machine transitions to a new state. The new active State is passed as the argument. -
private string m_name
Optional name given to the state machine instance (set via constructor). Exposed via the publicname
property. -
private State _currentState
Reference to the currently active State instance or null when the machine is not started.
Properties
-
public string name { get; }
Read-only property exposing the optional name assigned to the machine. -
public bool isStarted { get; }
True when the machine has an active (non-null) current state.
Constructors
public StateMachine(string name = null)
Creates a new StateMachine instance. Optionally accepts a name used for identification/debugging.
Methods
-
public virtual void Start(State initialState)
Starts the state machine with the supplied initialState. If the machine is not already started and initialState is non-null, the machine transitions into that state, invokes its TransitionIn, and then invokes theonStarted
callback. If the machine is already started an Exception ("already started") is thrown. -
public virtual void Stop()
Stops the state machine. If a current state exists it is asked to TransitionOut, then the current state reference is cleared and theonStopped
callback is invoked. Safe to call when not started (no-op). -
public void Update()
If started, calls the current state's Update() and inspects the returned State.Result. Behavior varies by ResultType: - Stop: calls Stop() on the machine.
- Transition: transitions to the provided
next
state. -
Continue: does nothing (remains in current state). The machine processes transitions immediately (TransitionTo is invoked from here when requested).
-
public void LateUpdate()
If started, delegates to the current state's LateUpdate(). -
public string GetCurrentStateName()
Returns the current state's Name (if set and non-empty), otherwise calls ToString() on the state. Returns "none" when there is no current state. -
public bool IsIn<T>() where T : State
Returns true if the current state is of type T. -
private void TransitionTo(State state)
Performs the internal transition: calls TransitionOut() on the previous state (if any), sets_currentState
to the new state, assigns this machine to the state'smachine
field, calls the new state's TransitionIn(), firesonTransitioned
with the new state, and immediately calls Update() to process any immediate result from the new state's Update().
Notes:
- The State type is not defined in this file; it is expected to provide TransitionIn(), TransitionOut(), Update() returning a State.Result (with ResultType Stop/Transition/Continue and possibly a next State), LateUpdate(), and a Name property and a machine
field/setter used by TransitionTo.
- The class uses Action callbacks for lightweight eventing. Some calls in the original code invoke an extension method Fire() on the Action (e.g., onStarted.Fire()). Ensure your environment provides that extension (or call the Action directly after null-check) when reusing this code.
Usage Example
// Example assumes a State base class exists with the expected members.
public class MyState : State
{
public override void TransitionIn() { /* entry logic */ }
public override void TransitionOut() { /* exit logic */ }
public override Result Update()
{
// return new Result(ResultType.Continue) etc.
}
public override void LateUpdate() { /* late frame logic */ }
}
// Create and use the state machine:
var sm = new StateMachine("ExampleMachine");
sm.onStarted += () => Debug.Log("State machine started");
sm.onStopped += () => Debug.Log("State machine stopped");
sm.onTransitioned += s => Debug.Log("Transitioned to: " + s.Name);
var initial = new MyState();
sm.Start(initial);
// In your update loop:
sm.Update();
sm.LateUpdate();
// Transition from within a state's Update by returning a Result with ResultType.Transition and a next State