State Management in Games
17 Nov 2012The title of this post does mention “Games”. In all honesty, with very minor modification you could change this implementation to suit any state requisition situation.
It’s the boring stuff that stops me half-way through developing a computer game. Building a menu, a title screen, a high-score chart.. you know, the stuff that takes you away from that big eye-candy feature that’s in the game.
Anyway, the implementation starts with a base class that manages some state. It’s a class that has provision to allow any derivatives of it to provide reaction code. The state manager will actually cause these “reactions” to occur. You’ll see this one shortly.
Here’s the class itself:
Of course, this is all nice and abstract. It doesn’t really do anything. Here’s a simple derivative. Don’t expect too much, it really doesn’t do that much at all:
Finally, we have the state machine. It is the kernel, the guy “in charge”. This will progress execution through the chain of states to ensure the story-board is presented to the user in such a way that they have a nice feeling of continuity throughout the game:
To really make this of immediate use to a games programmer, I suggest the reader takes this code as an example and implements the following necessities:
Time management
Giving the programmer the ability to tell how long it was between frame renders or even being able to control the number of frames processed per second is a requirement. This is the heartbeat/lifeblood to giving your program the ability to exist in a human (or non-human) consistent time scale.
Eventing
Most media libraries define some sort of eventing to allow the library to interact with the hosting operating system. These interrupts or notion of “important incoming information” needs to be embraced into the state framework as one of the most important side-effects can come from external interation (keyboards, mouse, etc).
Enjoy.