Jump to content
  • Advertisement
Sign in to follow this  
lucky6969b

About state machine

This topic is 2566 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


void CCB::Update(float dt)
{
if (IsFinishedTask())
{
SetState(CCB_PATHFIND);
(this->*Execute)(dt);
}
///...



void CCB::Update(float dt)
{
if (IsFinishedTask() )
{
switch(mState)
{
case CCB_WAIT:
SetState(CCB_PATHFIND);
break;

case CCB_PATHFIND:
/// blah blah
}
(this->*Execute)(dt);
}
///...


is this inevitable?

Is that I can't avoid the switch case (or inner switch statement) for changing states?
Or there is a better approach on doing this? It would be nice if anyone can show off a bit of state machine code?
Thanks
Jack

Share this post


Link to post
Share on other sites
Advertisement
That's effectively the sort of state machine I've most often needed to use. Of course, you can code it any old way, such as swapping in a different state object, or a state function pointer, or whatever. The decision usually depends on how the rest of your application has been structured, and whether you want all state handling in one file or want it spread across several source files or classes.

[source lang="cpp"]

//in main loop
pOnStateTick(this);
//...

//state function
void StateWait(MyApp *app)
{
if (DoWaitStuff() == FINISHED)
app->pOnStateTick = StatePathFind;
}

void StatePathFind(MyApp *app)
{
if (DoPathFindTick() == FINISHED)
app->pOnStateTick = StateWait;
}
[/source]

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!