Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You look at things as: </p> <p>I have a static container for states (your App), and a lot of states (you AppState) which can contain data and only one handler.</p> <p>Instead look at it as: </p> <p>I have a one StateMachine class. (I may or may not have many instances of it.) This holds the data needed to interact with the outside world. Is also contains a static set of eventHandlers, one for each state. These eventHandler classes contain no data.</p> <pre><code>class StateMachine { public: void handleInput() { //there is now only one dispatcher if( world.doingInput1() ) _current_state-&gt;handleInput1( *this ); else if( world.doingInput2() ) _current_state-&gt;handleInput2( *this, world.get_Input2Argument() ); //... } //the states, just a set of event handlers static const State&amp; state1; static const State&amp; state2; //... StateMachine( OutsideWorld&amp; world ) :world( world ) { setCurrentState( StateMachine::state1 ); } void setCurrentState( const State&amp; state ) { _current_state = &amp;state; } OutsidWorld&amp; world; private: State* _current_state; }; class State { public: //virtual ~State(); //no resources so no cleanup virtual void handleInput1( StateMachine&amp; sm ) const {}; virtual void handleInput2( StateMachine&amp; sm, int myParam ) const {}; //... }; class State1 { public: //define the ones that actually do stuff virtual void handleInput1( StateMachine&amp; sm ) const { sm.world.DoSomething(); sm.setCurrentState( StateMachine::state27 ); } virtual void handleInput27( StateMachine&amp; sm, int myParam ) const { sm.world.DoSomethingElse( myParam ); }; }; const State&amp; StateMachine::state1 = *new State1(); //... more states </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload