Friday, June 22, 2007
Friday, June 22, 2007
0 Comments

I never got around to writing my own state machine class, and I could have used one recently, so I did a little digging around. I found a site with a decent example of a state machine as a class.
Often finite state machines are implemented (in real projects and tutorials) as a mass of code in a single class, usually a giant switch statement hundreds or (in one instance I’ve seen) thousands of lines long. A simple switch statement is great for a simple agent with two or three states, but the more complex the agent gets, both in the number and complexity of the states, the more complex the code gets. Using a switch statement also offers very little opportunity to reuse code across different agents and different projects (other than by cutting and pasting).Pretty cool and a good jumping off point. With this little bit of stuff, when I get time, I'll probably whip up a fairly robust state machine class for use on different projects. Every single project I work on has to have some level of state, and lately they have been getting more complex. It will help greatly in having such a class available to handle stuff. Cheers Big Room Ventures Ltd.
The solution I’ve used in a number of projects is to implement each state as a separate class. The class will contain all the code necessary for entering, updating and exiting that state and nothing else. This way, the code for each state is separate and the agent code isn’t cluttered by it.






