Jump to content
  • Advertisement
Sign in to follow this  
metimmee

2d Lookup table for state machine

This topic is 4253 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 need to implement a 2d lookup table for my state machine. It needs to be efficient and variable in size. The lookup table will hold CEvent objects. I have ruled out a 2d array and a 1d vector with x * y elements since the lookup table needs to be dynamic in size. I have considered a vector containing structures which contain the CEvent object and another structure containing the 2 lookup states (new state/current state) and overloading operator == to check the state structure to allow searching. My app uses STL containers. Am I on the right track, or is there a better/easier way? Thanks

Share this post


Link to post
Share on other sites
Advertisement
It's a good aproach, at least it's how I'd do it.

The only thing you need to consider is when you fire the event, on inbound connections (before entering state) or outbound (after leaving the state), and if you possibly want to support both. There's cases where each aproach can be useful.

You can make a 2D table containing CEvent. This can be even extended to support both types of events, std::pair< CEvent, CEvent >. The lookup is then simply array[curr_state][next_state], and the array is a std::vector of vectors.

There is no need to rule out the arrays, since they are the most straightforward aproach if you're using integer tokens.

For arbitrary tokens a map might work, and that even allows you to to use map::find == map::end criteria to determine error state. Use something like std::pair< int, int> as your key, where ints are current and next token. But for complex transitions, the lookup will be slower than a table.

Share this post


Link to post
Share on other sites
The pressing question is, does the table need to be rectangular?

If yes, use boost::multi_array.

If no (also frameable as "it needs *not* to be rectangular" ;) ), use a std::vector of std::vectors to represent it.

Share this post


Link to post
Share on other sites
Thanks for the replies.

What I am doing just now is refactoring an existing project in which the state machine is plumbed into a complex simulation project. The complexity comes from the fact that there are over 100 state machines within the main state machine. Some state machines are affected by others within the main state machine. I have also found a use for what I call 'Variable Atoms', these are literally variables that are affected by state changes, where their values or valid ranges are changed, dependent on what events have been posted.

The state machine is serialized into an XML file and loaded at run-time. I have a graphical editor which allows simulation and debug of the state machine. One of the features which I have found most powerful is the ability to change behavior significantly without having to change a line of code. Bug reports come in and on several occasions, after further testing, the XML file is emailed to the customer; job done. There is also a report generator which generates tabulated html documentation.

As a side-note. Would it be worth going to the effort of making this project generic so that I could release it open source as an add-on for other projects?


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!