State Machine Implementation

Started by
1 comment, last by Trap 17 years, 10 months ago
Greetings! I was wondering if there is allready a simple C++ State Machine implemented allready? I would need just some interfaces for:

class IState
{
public:
  virtual void Enter() = 0;
  virtual void Exit() = 0;
  virtual void Input() = 0;
  virtual void Transition() = 0;
};

class StateMachine
{
private:
  std::vector< IState* > m_States;
  IState* m_pActive;
public:
  void Add( IState* pState ) { m_States.push_back( pState ); }
  void Select( IState* pState ) { m_pActive = pState; }
  void Pulse( void ) { m_pActive->Input(); }
};
The question is, is there any predesigned template that I could use? My problem is, how should the IState manage to change a State? It must call the StateMachine or perhaps does it need to return a number or something like that? I would like to achive reusable code altough Input() might mislead that it gets called when a player hits a key or mouse or joystick device? What Actions should be implemented and which parameters should be passed? Thank you in advance!
Advertisement
I believe in AI game programming Gems I, there is a good state machine implementation.
A meta-level remark:
You know what hardware development guys do when they need a (complex) state machine? They use simple embedded CPUs that run the state machine (the program the CPU runs is the state machine).

What you sketched in your post is an interpreter on top of a state machine (the CPU) that interprets state machines. The good old "adding one layer of indirection solves any problem except too many levels of indirection" ;)
If your state machine is performance critical compiling the state machine description to machine code is the way to go.

[Edited by - Trap on June 10, 2006 1:27:25 PM]

This topic is closed to new replies.

Advertisement