Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is it possible to set initial state (at runtime) to something which is different from what was passed in FSM_INITIAL_STATE? #34

Open
husnaini798 opened this issue Jul 4, 2022 · 1 comment
Labels

Comments

@husnaini798
Copy link

Is it possible to change the initial_state at runtime?

I am storing the the states of my machine in the database and at restart, I would like to start from the last known stored state. Is it possible to set the FSM_initial_state at runtime, without using the transit function?

Thanks

@digint
Copy link
Owner

digint commented Jul 4, 2022

I would not recommend this, as you are breaking basic state machine rules with this.
That said, you could probably write your own set_initial_state specialization, something like:

#define FSM_INITIAL_STATE_FROM_DB(_FSM, _STATE_1, _STATE_2)  \
namespace tinyfsm {                                                   \
  template<> void Fsm< _FSM >::set_initial_state(void) {              \
    if(dbvalue == 1) \
      current_state_ptr = &_state_instance< _STATE_1 >::value;            \
    else if(dbvalue == 2 \
      current_state_ptr = &_state_instance< _STATE_2 >::value;            \
  }                                                                   \
}

You can "hard" set a state at any time by modifying current_state_ptr.

@digint digint added the question label Jul 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants