This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
0 votes

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

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?

Godot version Godot 3.3.3 - Standard Version
in Engine by (96 points)

1 Answer

+1 vote
Best answer

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.

by (1,656 points)
selected by

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

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.