Jump to content
  • Advertisement
Sign in to follow this  
5quirrel

Game states problem. Require help

This topic is 3834 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

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)

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

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!