Jump to content

View more

Image of the Day

#ld38 #screenshotsaturday Mimosa Fizz action gif #2 https://t.co/TUzdppvfUL
IOTD | Top Screenshots

The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

Sign up now

How to "reset" a singleton?

4: Adsense

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
4 replies to this topic

#1 c_fried_rice   Members   


Posted 22 February 2014 - 06:22 AM

This may come off as rather vague but I've been awake too long, and will post more info later if needed.


I have a state machine that deals with an abstract State class. Inherited from this are several Singleton gamestate classes. However, players will be able to transition back to certain states, and when they do I want the state to be totally reset - as if it were refreshed.


How would I go about doing this?


Thanks a ton!

#2 Danicco   Members   


Posted 22 February 2014 - 06:55 AM

Rather than resetting a singleton and potentially breaking something that depends on it, it could be better to keep two of them - a previousState and a currentState.

Making your class able to deal with states changes like this so you won't have to worry about re-building the previous state if you're going back and wants things to be exactly as they were.


If that doesn't work for you, you can implement a function in the abstract class that the states resets themselves. So if the inherited states are very different in their core somehow they'll need to know how to reset.


But if I may suggest another approach... I would suggest to avoid using Singletons at all.

There's a bunch of topics on why they're bad (and the good bits as well) that you can look up, but generally, there's no reason at all to keep a singleton. With a good code design you shouldn't need to.

#3 Scourage   Members   


Posted 22 February 2014 - 09:36 AM

I have to agree with Danicco, this does not sound like a good use case for Singletons.  Maybe your gamestate manager might be a singleton, but the individual game states probably shouldn't be.  


Your gamestates probably should inherit an abstract base class with the following functions (pseudo code follows)

class AbstractGameState
  void onEnter() =0;
  void onTick(Time dt)=0;
  void onExit()=0;

Now your derived classes implement these functions and as you transition between states, you call onExit() of the state you're leaving and then onEnter() on the state you're going in to.  OnExit() will give your states the opportunity to either save state for when you come back to that particular state, if thats what you want. OnEnter() will give your states the opportunity to either reload a saved state, or reset the state, again depending on what you want.


Inside your game state manager, you can have a transition function to manage this for you:

void transition(AbstractGameState* toState)




Edited by Scourage, 22 February 2014 - 09:38 AM.

Halfway down the trail to Hell...

#4 dejaime   Members   


Posted 24 February 2014 - 12:06 AM

If you ever need to reset a singleton, you probably shouldn't be using singletons.


I don't know if I didn't understand your problem, but this may help: http://en.wikipedia.org/wiki/Strategy_pattern

Edited by dejaime, 24 February 2014 - 12:13 AM.

#5 haegarr   Members   


Posted 24 February 2014 - 03:17 AM

I think the OP means that e.g. each time the GameMenuState state is entered, it should show the main menu regardless of at which level the menu was left in a potential previous call. This would have nothing to do with reconstruction of a former runtime value after resetting it to an initial value. If I want the runtime value to persists I just need to do nothing!


That said, if the state objects have enter/exit handlers like Scourage has mentioned, implementing the enter handler of the respective states so that they initialize all of their internal values will do the trick. Other states, like e.g. the GameplayState state, will not do so (or they do so only on their first enter) and hence simply continue when being re-entered.


However, whether implementing game states as singletons is a good idea is a totally other question. Personally I agree with Danicco and would not implement them as singletons.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.