/// <summary> /// This method tries to change the state the FSM is in based on /// the current state and the transition passed. If current state /// doesn't have a target state for the transition passed, /// an ERROR message is printed. /// </summary> public void PerformTransition(Transition trans, bool ifGlobal) { // Check for NullTransition before changing the current state if (trans == Transition.NullTransition) { return; } // Check if the currentState has the transition passed as argument StateID id; if (!ifGlobal) { id = CurrentState.GetOutputState(trans); } else { id = GlobalState.GetOutputState(trans); } if (id == StateID.NullStateID) { Debug.LogError("FSM ERROR: State " + CurrentStateID.ToString() + " does not have a target state " + " for transition " + trans.ToString()); return; } // Update the currentStateID and currentState CurrentStateID = id; foreach (FSMState <T> state in states) { if (state.ID == CurrentStateID) { // Do the post processing of the state before setting the new one CurrentState.DoBeforeLeaving(); CurrentState = state; // Reset the state to its desired condition before it can reason or act CurrentState.DoBeforeEntering(); break; } } } // PerformTransition()
public void ResetToDefaultState() { CurrentState = states[0]; CurrentStateID = states[0].ID; CurrentState.DoBeforeEntering(); }