tinyfsm icon indicating copy to clipboard operation
tinyfsm copied to clipboard

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

Open husnaini798 opened this issue 3 years ago • 1 comments

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

husnaini798 avatar Jul 04 '22 06:07 husnaini798

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 avatar Jul 04 '22 19:07 digint