Scene Transition + Timer issue

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By JCNegar

Hello there everyone, It’s going to be a bit long but bear with me :smiley:

So I’ve been making this game lately, Where the character moves a lot between different house rooms (Each room is a scene). My approach to that (although I think I’ve done it in the worst way possible), was to just make a “Player” instance in each of the rooms, That way, I’ll just have to switch the room and everything works fine. Now, I want to add a timer, that’ll be decreasing during the WHOLE GAME (the player has 20 minutes to finish the game). I couldn’t find a way to make this because as I told you each room is a scene, and I keep swapping them, so, It just doesn’t work.

What I’ve tried to do ===
I’ve tried to instance all the scenes in a “Game” node2d, but there is a big problem with that, because now there are many player instances at once.

Any ideas?

:bust_in_silhouette: Reply From: AlexTheRegent

You can use singleton to carry your time between scenes. Let’s say that your timer can be referenced as $Timer. Then, before calling get_tree().change_scene() you simply save current time_left of timer to your singleton, i.e. something like NameOfSingleton.timer_time_left = $Timer.time_left. And in every scene you have setup timer in _ready() as $Timer.start(NameOfSingleton.timer_time_left).
Also do not forget to reset timer to 20 minutes each time player start new game (NameOfSingleton.timer_time_left = 20 * 60).

Note that this is one of the possible solutions. In your case of root “Game” node you just need to implement your own scene switching algorithm, i.e. delete previous one and adding new one. This way you will have only one player at the time.

Thanks! I managed to make it work and now I have a good understanding of AutoLoads.

JCNegar | 2021-09-09 17:25