Game states problem. Require help

Started by
2 comments, last by bombinator-dev 15 years, 10 months ago
Good Day to all, I wish to implement a game state manager for different states of a program. I looked through the form and other available websites. What confuses me is the use of singletons in some of the examples. For what i have learnt, i could have a vector of functions for each state, and a state manager class. Or i could have 2 classes, game state and game state manager, then i would inherit the game state class and create singletons and pass them into the manager vector list. Which is a better alternative for making a game state? Is creating singletons the same as making a global instance?(such that it uses up the RAM)
Advertisement
Hi 5quirrel.

Yes, a singleton is almost the same as a global instance.

A singleton should be able to create itself, and when I write a singleton in C# I call the class directly like this: TheSingleton.Get().SetGameState(gsPaused);

Where "TheSingleton" is the class, "Get()" is a static method that can be called even if the class has never been instantiated before.

"SetGameState()" is a standard public method.


When you call the "Get" method the class will check an internal value again null, and create an instance of itself if null, that way the singleton will create ifself the first time it is called to and you will never have to worry about either null references or two classes having different instances of the class.
Why do you even want singletons here? I don't see what problem it is solving.
Quote:Original post by 5quirrel
Or i could have 2 classes, game state and game state manager, then i would inherit the game state class and create singletons and pass them into the manager vector list.


You don't need singletons here, just create and instance of your state and pass that to the game state manager. A lot of code I have seen (and mine actually) has the game state manager as a singleton (although it's not necessary) but I've never seen the states themselves be singletons.

This topic is closed to new replies.

Advertisement