I wrote a finite state machine template, because I want to write different FSM 1. status is replaced by enumeration (for debugging convenience) 2. to run FSM, you only need setState and updateState (float delta_time. use GetState to obtain the current status. 4. many conversions are based on timing, So I implemented the GetTimeInCurState () method 5. execute specific actions in these methods BeginState EndState UpdateState [cpp] // (c) Francois Guibert, www.frozax.com (@ Frozax) # pragma once template <typename T> class fgsm {public: fgsm (): _ time_in_cur_state (0.0f), _ cur_state (-1) {} virtual void BeginState (T s Tate) {} virtual void UpdateState (T state) {} virtual void EndState (T state) {} void SetState (T state) {EndState (T) _ cur_state ); _ cur_state = state; _ time_in_cur_state = 0.0f; BeginState (T) _ cur_state);} void UpdateFSM (float delta_time) {if (_ cur_state! =-1) {_ time_in_cur_state + = delta_time; UpdateState (T) _ cur_state) ;}} float GetTimeInCurState () {return _ time_in_cur_state;} T GetState () {return (T) _ cur_state;} private: float _ time_in_cur_state; int _ cur_state;}; // (c) Francois Guibert, www.frozax.com (@ Frozax) # pragma once template <typename T> class fgsm {public: fgsm (): _ time_in_cur_state (0.0f), _ cur_state (-1) {} virtual void BeginS Tate (T state) {} virtual void UpdateState (T state) {} virtual void EndState (T state) {} void SetState (T state) {EndState (T) _ cur_state); _ cur_state = state; _ time_in_cur_state = 0.0f; BeginState (T) _ cur_state);} void UpdateFSM (float delta_time) {if (_ cur_state! =-1) {_ time_in_cur_state + = delta_time; UpdateState (T) _ cur_state) ;}} float GetTimeInCurState () {return _ time_in_cur_state;} T GetState () {return (T) _ cur_state;} private: float _ time_in_cur_state; int _ cur_state;}; usage: first sets up the State enumeration to be applied, for example, [cpp] enum EState {STT_OFF =-1, // optional,-1 is the initial state of the fsm STT_WALK, STT_RUN, STT_STOP, STT_EAT }; enum EState {STT_OFF =-1, // optio Nal,-1 is the initial state of the fsm STT_WALK, STT_RUN, STT_STOP, STT_EAT}; then inherit class fgsm [cpp] class ObjectUsingFSM: public fgsm <EState> {public: //... void UpdateState (EState t); void BeginState (EState t); void EndState (EState t );//...}; class ObjectUsingFSM: public fgsm <EState> {public ://... void UpdateState (EState t); void BeginState (EState t); void EndState (EState t ); //...}; Conclusion: You can use the code for free in your project, which is very simple and commonly used. In addition, you can add getprviusstate () to EndState () as needed in the future () getNextState () and so on...