Jump to content
  • Advertisement
Sign in to follow this  

Is this a good idea? (Game States, Paper)

This topic is 2835 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 was looking at this paper on Game State management
"An Architecture For Game State Management Based On State Hierarchies


I decided I don't really need restrictions on whether changes or pushes of states are 'restricted' by the state definition itself so the hierarchical part is kind of a moot point for me. I will just trust myself to call change or push when appropriate.

The question though is about the singleton implementation.

I started with designating states by enums.

enum Mode{ OFF, INTRO, MAIN, RUNNING, ... };

and creating a 'Mode' variable in a 'Context' object existing in a stack of 'Contexts'.

Pushing results in a new context being pushed on the context stack.

Changing results in the stack being cleared and the new Context being put on the stack.

The current Context can have function pointers to which loop function needs to be called while in that state.

This results in functions hanging out in the namespace not explicitly related to the enum Mode but named things like IntroLoop() to remind that it belongs to the INTRO enum.

Fine and all, but I was wondering if going to Singleton for this case as in the above paper (creating singleton state definitions) and maybe using templates is advisable to make it more general avoiding switches reliant on passing enums around?

Share this post

Link to post
Share on other sites

Fine and all, but I was wondering if going to Singleton ... is advisable...?
Using a singleton is never advisable ;P

To avoid tying specific state implementations with the state system itself, anything to do with "intro", "main", etc should be kept far away from the core state-managing code. You could do this by making the enum a template argument.// engine code
template<class T> class StateManager

// game code
enum Mode { ... }
typedef StateManager<Mode> MyGameStateManager;
Another (IMO more preferable) way is to use an interface:// engine code
class IState
virtual void OnUpdate() = 0;
class StateManager
void Push( IState& );

// game code
class InitState : public IState { void OnUpdate() { ... } };
InitState initState;
StateManager m;
m.Push( initState );

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!