SimpleStateMachineT Class |
Namespace: KSPDev.ProcessingUtils
public sealed class SimpleStateMachine<T> where T : struct, new(), IConvertible
The SimpleStateMachineT type exposes the following members.
Name | Description | |
---|---|---|
![]() ![]() | SimpleStateMachineT | Constructs a new uninitialized state machine. |
Name | Description | |
---|---|---|
![]() ![]() | currentState | Current state of the machine. |
![]() ![]() | isStrict | Tells if all the transitions must be explicitly declared. |
![]() | machineIsInactive | Tells if the machine was shutdown. |
Name | Description | |
---|---|---|
![]() ![]() | AddStateHandlers | Adds a state change event. |
![]() ![]() | CheckCanSwitchTo | Verifies if the machine can move into the desired state. |
![]() | RemoveHandlers | Removes a state change event handler. |
![]() | ResetTransitionConstraint | Clears the transitions for the source state if any. |
![]() ![]() | SetTransitionConstraint | Defines a state and the allowed target states for it. |
Name | Description | |
---|---|---|
![]() ![]() | onAfterTransition | Event that fires when the state machine has changed its state. |
![]() ![]() | onBeforeTransition | Event that fires before the state machine has changed its state. |
/// <summary>A class that demonstrates a simple state module with three states.</summary> /// <remarks> /// There are the following rules for the state changes: /// <list type="bullet"> /// <item>The state <c>One</c> can be transitioned into both <c>Two</c> and <c>Three</c>.</item> /// <item>The states <c>Two</c> and <c>Three</c> can only return back to <c>One</c>.</item> /// <item>In states <c>Two</c> and <c>Three</c> different menu options are available.</item> /// <item>In state <c>One</c> no menu options are available.</item> /// </list> /// </remarks> class SimpleStateMachine1 : PartModule { public enum State { One, Two, Three } [KSPField(isPersistant = true)] public State persistedState = State.One; // ALWAYS provide a default value! SimpleStateMachine<State> stateMachine; [KSPEvent(guiName = "State: TWO")] public void StateTwoMenuAction() { Debug.Log("StateTwoMenuAction()"); } [KSPEvent(guiName = "State: THREE")] public void StateThreeMenuAction() { Debug.Log("StateThreeMenuAction()"); } public override void OnAwake() { stateMachine = new SimpleStateMachine<State>(strict: true); // State ONE can be transitioned into both TWO and THREE. stateMachine.SetTransitionConstraint( State.One, new[] {State.Two, State.Three}); // State TWO can only get back to ONE. stateMachine.SetTransitionConstraint( State.Two, new[] {State.One}); // State THREE can only get back to ONE. stateMachine.SetTransitionConstraint( State.Three, new[] {State.One}); // No menus available in state ONE. stateMachine.AddStateHandlers( State.One, enterHandler: oldState => { Events["StateTwoMenuAction"].active = false; Events["StateThreeMenuAction"].active = false; }, leaveHandler: newState => Debug.LogFormat("Move from ONE to {0}", newState)); // Only TWO-menu is available in the state TWO. stateMachine.AddStateHandlers( State.Two, enterHandler: oldState => { Events["StateTwoMenuAction"].active = true; Events["StateThreeMenuAction"].active = false; }); // Only THREE-menu is available in the state THREE. stateMachine.AddStateHandlers( State.Three, enterHandler: oldState => { Events["StateTwoMenuAction"].active = false; Events["StateThreeMenuAction"].active = true; }); } public override void OnStart(PartModule.StartState state) { stateMachine.currentState = persistedState; // Restore state from the save file. } void OnDestory() { // Usually, this isn't needed. But if code needs to do a cleanup job it makes sense to wrap // it into a handler, and stop the machine in the Unity destructor. stateMachine.currentState = null; } public override void OnUpdate() { if (Input.GetKeyDown("1")) { // This transition will always succceed. stateMachine.currentState = State.One; } if (Input.GetKeyDown("2")) { // This transition will only succceed if current state is MyState.One. stateMachine.currentState = State.Two; } if (Input.GetKeyDown("3")) { // This transition will only succceed if current state is MyState.One. stateMachine.currentState = State.Three; } } }