A simple and usable Finite State Machine (FSM) in c ++

Source: Internet
Author: User

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...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.