Click or drag to resize

SimpleStateMachineTSetTransitionConstraint Method

Defines a state and the allowed target states for it.

Namespace:  KSPDev.ProcessingUtils
Assembly:  KSPDev_Utils.2.5 (in KSPDev_Utils.2.5.dll) Version: 2.5 for KSP v1
Syntax
C#
public void SetTransitionConstraint(
	T fromState,
	T[] toStates
)
Request Example View Source

Parameters

fromState
Type: T
The source state.
toStates
Type: T
The list of the states that are allowed as the targets.
Remarks
In the strict mode it's required that every transition is declared explicitly.
Examples
class SimpleStateMachineStrict {
  public enum State {
    One,
    Two,
    Three
  };

  public void TestMachine() {
    var sm = new SimpleStateMachine<State>(strict: true);
    sm.SetTransitionConstraint(State.One, new State[] { State.Two });
    sm.AddStateHandlers(
        State.One,
        enterHandler: newState => LogFromTo(sm.currentState, newState),
        leaveHandler: newState => LogFromTo(sm.currentState, newState));

    sm.currentState = State.One;  // Start the machine.
    // Logs: Move from NULL to One

    if (sm.CheckCanSwitchTo(State.Two)) {
      sm.currentState = State.Two;  // An allowed tranistion.
      // Logs: Move from One to Two
    }

    if (sm.CheckCanSwitchTo(State.Three)) {
      // This will never happen.
      sm.currentState = State.Three;
    }

    sm.currentState = null;  // Stop the machine.
    // Logs: Move Two to NULL
  }

  void LogFromTo(State? from, State? to) {
    Debug.LogFormat(
        "Move from {0} to {1}", DbgFormatter.Nullable(from), DbgFormatter.Nullable(to));
  }
}
See Also