Jump to content
  • Advertisement
Sign in to follow this  
Samurai Jack

State Machine Implementation

This topic is 4421 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
Advertisement
I believe in AI game programming Gems I, there is a good state machine implementation.

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!