Skip to content

Game.Rendering.Utilities.State

Assembly: Assembly-CSharp
Namespace: Game.Rendering.Utilities

Type: abstract class

Base: System.Object

Summary:
Base class representing a state used by a StateMachine in the rendering utilities. Provides a small lifecycle surface (TransitionIn, Update, LateUpdate, TransitionOut) and a nested Result/ResultType mechanism to indicate whether the state should continue, stop the machine, or transition to another State. The class also exposes a machine property that is set once by the StateMachine; attempting to set it a second time throws an exception. The Name property is a simple string label you can use for debugging or identification.


Fields

  • private StateMachine _machine
    Holds a reference to the StateMachine that owns this State. The machine property wraps this field; after the StateMachine sets this field once, subsequent sets throw an Exception (property is effectively read-only after first set).

  • (nested types) public enum ResultType and public struct Result
    ResultType is an enum with values Continue, Stop and Transition. Result is a small struct that contains a ResultType, an optional next State (for transitions), convenience boolean properties (isContinue, isStop, isTransition), and static factory members (Continue, Stop, TransitionTo).

Properties

  • public StateMachine machine { get; set; }
    Gets the StateMachine reference for this state. The setter is guarded: if the underlying field is already non-null, setting it again throws an Exception with message "property is read only after first set". Typically the StateMachine will set this once when the state is registered/entered.

  • public string Name { get; set; }
    Simple get/set property for an optional name or label for the state, useful for debugging and logging.

Constructors

  • public State()
    Default constructor. No special initialization is required by the base class. Subclasses can initialize their own data.

Methods

  • public virtual void TransitionIn()
    Lifecycle hook invoked when the state becomes active. Default implementation is empty. Override to perform initialization or to start animations/operations on state entry.

  • public virtual Result Update()
    Called to update the state's logic. Default implementation returns Result.Continue. Override to perform per-frame or per-tick logic and return one of:

  • Result.Continue — keep running this state
  • Result.Stop — request the machine to stop
  • Result.TransitionTo(someState) — transition to another state

  • public virtual void LateUpdate()
    Optional late-update hook. Default implementation is empty. Useful for logic that must run after regular Update.

  • public virtual void TransitionOut()
    Lifecycle hook invoked when the state is leaving. Default implementation clears the internal machine reference (_machine = null). Override to clean up resources; if overriding, consider calling base.TransitionOut() to clear the machine reference unless you manage it differently.

Usage Example

// Example subclass demonstrating Update returning different Results.
public class ExampleState : State
{
    public ExampleState()
    {
        Name = "ExampleState";
    }

    public override void TransitionIn()
    {
        // Called when this state becomes active
        UnityEngine.Debug.Log($"{Name} entered");
    }

    public override Result Update()
    {
        // Example logic: after some condition, transition to another state
        if (ShouldStop())
        {
            return Result.Stop;
        }
        if (ShouldSwitchState())
        {
            State next = new AnotherState();
            return Result.TransitionTo(next);
        }

        // Default: keep running this state
        return Result.Continue;
    }

    public override void TransitionOut()
    {
        // Clean up
        UnityEngine.Debug.Log($"{Name} exiting");
        base.TransitionOut(); // clears the machine reference
    }

    private bool ShouldStop() { /* ... */ return false; }
    private bool ShouldSwitchState() { /* ... */ return false; }
}

Notes: - Use Result.TransitionTo(newState) to request a transition and supply the next state instance. - The StateMachine should set the machine property once; do not attempt to set it again from user code. - Name is optional but helpful for logging and diagnostics.