Sign in to follow this  

State Machine Implementation

This topic is 4208 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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!

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites

This topic is 4208 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this