Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I have implemented a small state machine in this way (in pseudo code):

class Input {}

class KeyInput inherits Input
{ 
   public :
     enum { Key_A, Key_B, ..., }
}

class GUIInput inherits Input
{ 
   public :
     enum { Button_A, Button_B, ..., }
}

enum Event { NewGame, Quit, OpenOptions, OpenMenu }

class BaseState
{
   String name; 
   Event get_event (Input input);
   void handle (Event e); //event handling function
}

class Menu inherits BaseState{...}
class InGame inherits BaseState{...}
class Options inherits BaseState{...}

class StateMachine
{
   public :

      BaseState get_current_state () { return current_state; }

      void add_state (String name, BaseState state) { statesMap.insert(name, state);}

      //raise an exception if state not found
      BaseState get_state (String name) { return statesMap.find(name); } 

      //raise an exception if state or next_state not found
      void add_transition (Event event, String state_name, String next_state_name)
      { 
         BaseState state = get_state(state_name);
         BaseState next_state = get_state(next_state_name);

         transitionsMap.insert(pair<event, state>, next_state);

      }

      //raise exception if couple not found
      BaseState get_next_state(Event event, BaseState state) 
      { 
         return transitionsMap.find(pair<event, state>); 
      }

      void handle(Input input) 
      { 
         Event event = current_state.get_event(input)
         current_state.handle(event);
         current_state = get_next_state(event, current_state);

      }

   private :

      BaseState current_state;
      map<String, BaseState> statesMap; //map of all states in the machine

      //for each couple event/state, this map stores the next state
      map<pair<Event, BaseState>, BaseState> transitionsMap; 

}

So, before getting the transition, I need to convert the key input or GUI input to the proper event, given the current state; thus the same key 'W' can launch a new game in the 'Menu' state or moving forward a character in the 'InGame' state;

Then I get the next state from the transitionsMap and I update the current state

Does this configuration seem valid to you ? Is it a good idea to separate states and transitions ?

And I have some kind of trouble to represent a 'null state' or a 'null event'; What initial value can I give to the current state and which one should be returned by get_state if it fails ?

share|improve this question

closed as primarily opinion-based by Josh Petrie Jul 8 at 3:16

Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise.If this question can be reworded to fit the rules in the help center, please edit the question.

1  
It seems like it's complicating matters, to me. Is there any reason you can't just give each state a next_state and previous_state member, and have them check in their update method whether they should transition or not? –  K.G. Mar 26 '12 at 21:20
    
I would expound upon what I mean in an answer, but I don't think I could answer better than this related post. –  K.G. Mar 27 '12 at 0:19

Browse other questions tagged or ask your own question.