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

## 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 on other sites
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]

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 15
• 21
• 21
• 11
• 9