How to keep my stopwatch's continuity across scenes?

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

For starters, I’m an absolute beginner. Second, I do already know about autoload but I tried to do that and it didn’t seem to work.

So basically I want a clock to record your time in my level and then display it in the game over screen so a player can beat their best time. I followed a stopwatch tutorial on youtube and wrote this code: Imgur: The magic of the Internet stopwatch code (the only thing I changed was setting timer_on = true, since in the tutorial it only turned on and off with a button, whereas I just wanted it to run automatically). Anyway, even when I put it in the auto-loader, it was resetting between scenes. It worked perfectly in the level (start at 0, count the seconds), but when it got to the win screen it would still start from 0, instead of continuing the count from where it left off in the level (I hadn’t programmed in the freeze yet, I just wanted to make sure there was continuity between scenes).

:bust_in_silhouette: Reply From: Enfyna

The easiest method would be creating a autoloaded Global script and giving it a time variable. When your game ends you would basically save it in the Global and after the scene change you would read it from the win screen and start from there.

 ...
 # Save time before scene change
 Global.game_time = time
 change_scene(...)

After the scene change in the win screen:

func ready():
    time = Global.game_time #Start the time from the saved time

I haven’t been able to test this yet because I broke a bunch of other things that I’m now stuck on, but it looks like exactly what I need, thank you!

sheathimony | 2023-04-06 13:33