Click or drag to resize

SimpleStateMachineTAddStateHandlers Method

Adds a state change event.

Namespace:  KSPDev.ProcessingUtils
Assembly:  KSPDev_Utils.2.5 (in KSPDev_Utils.2.5.dll) Version: 2.5 for KSP v1
Syntax
C#
public void AddStateHandlers(
	T state,
	SimpleStateMachineTOnChange enterHandler = null,
	SimpleStateMachineTOnChange leaveHandler = null,
	bool callOnInit = true,
	bool callOnShutdown = true
)
Request Example View Source

Parameters

state
Type: T
The state to call a callback on.
enterHandler (Optional)
Type: KSPDev.ProcessingUtilsSimpleStateMachineTOnChange
The callback to call when the state machine has switched to a new state. The callback is triggered after the state has actually changed. The callback's parameter is the old state, from which the machine has switched.
leaveHandler (Optional)
Type: KSPDev.ProcessingUtilsSimpleStateMachineTOnChange
The callback to call when the state machine is going to leave the current state. The callback is triggered before the state has actually changed. The callback's parameter is the new state, to which the machine is going to switch.
callOnInit (Optional)
Type: SystemBoolean
Tells if this handler is allowed to be called when the state machine initiates from the null state. This usually means the owning object is in process of loading its state. Not all functionality can be available at this moment.
callOnShutdown (Optional)
Type: SystemBoolean
Tells if this handler is allowed to be called when the state machine goes into the null state. This usually means execution of the cleanup code, and the state of the owning object can be reduced.
Remarks

When the state is changed by setting the currentState property, the transition callbacks are only called when the state has actually changed.

Note, that the code must not expect that the handlers will be called in the same order as they were added. Each handler must be independent from the others.

Examples
class SimpleStateMachineFree {
  public enum State {
    One,
    Two,
    Three
  };

  public void TestMachine() {
    var sm = new SimpleStateMachine<State>(strict: false);
    sm.AddStateHandlers(
        State.One,
        enterHandler: oldState => Debug.Log("Now in state ONE"),
        leaveHandler: newState => Debug.LogFormat("Going into state: {0}", newState));
    sm.onBeforeTransition += (from, to) => Debug.LogFormat(
        "Before move: current={0}, new={1}",
        DbgFormatter.Nullable(sm.currentState), DbgFormatter.Nullable(to));
    sm.onAfterTransition += (from, to) => Debug.LogFormat(
        "After move: old={0}, current={1}",
        DbgFormatter.Nullable(from), DbgFormatter.Nullable(sm.currentState));

    sm.currentState = State.One;  // Start the machine.
    // Logs:
    // Now in state ONE
    // Before move: current=NULL, new=One
    // After move: old=NULL, current=One

    sm.currentState = State.Two;
    // Logs:
    // Going into state: Two
    // Before move: current=One, new=Two
    // After move: old=One, current=Two

    sm.currentState = State.Three;
    // Logs:
    // Before move: current=Two, new=Three
    // After move: old=Two, current=Three

    sm.currentState = null;  // Stop the machine.
    // Logs:
    // Before move: current=Three, new=NULL
    // After move: old=Three, current=NULL
  }
}
See Also