Jump to content
  • Advertisement
Sign in to follow this  
lucky6969b

How to design a state machine?

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

I'd like to define a state machine that returns the next state of an entity for animation purposes. Where should I be starting to look at? [Newbie]
Thanks
Jack

Share this post


Link to post
Share on other sites
Advertisement
Hidden
Hi rip-off, Thank you for your response. Let me give you a bit of details and see I or we can come up with some ideas. The NPCs I am running have several states IDLE, PATHFINDING, PICKING, SEARCHING, the current coding looks like this switch(current_state) { case IDLE: switch (sub_state) { case GOODS_ARRIVE: return PATHFINDING; //..// case PATHFINDING: switch (sub_state) { case OPERATOR_ARRIVE_ON_SITE: return ATTACHING; case OPERATOR_LEAVE_SITE: return DETACHING; } } Am I on the right track, or there is improvement to it I can make. Thanks Jack

Share this post


Link to post
Hi rip-off,
Thank you for your response. Let me give you a bit of details and see I or we can come up with some ideas.
The NPCs I am running have several states IDLE, PATHFINDING, PICKING, SEARCHING, the current coding looks like this

switch(current_state)
{
case IDLE:
switch (sub_state)
{
case GOODS_ARRIVE:
return PATHFINDING;
//..//
case PATHFINDING:
switch (sub_state)
{
case OPERATOR_ARRIVE_ON_SITE:
return ATTACHING;
case OPERATOR_LEAVE_SITE:
return DETACHING;
}
}

Am I on the right track, or there is improvement to it I can make.
Thanks
Jack

Share this post


Link to post
Share on other sites

Am I on the right track, or there is improvement to it I can make.


Don't hard code it, create a data driven system, so you can write state machine data (states, actions/transitions, conditions) in a data file, so you can edit the state machine without code rebuilding.

Share this post


Link to post
Share on other sites
and if it"s a state machine that update often, you can change remove the switch and uses funtion pointer instead :


typedef void (Machine::*STATE)(float); ///< this is a state for state machine
#define VS_IDLE &Machine::stateIdle
...
STATE mState; ///< this is the current state, be sure it's never null

and in you source :

void update(float iDeltaTime){
(this->*mState)(iDeltaTime);
}

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!