#Timer Through Scenes

63 messages · Page 1 of 1 (latest)

mossy island
#

I'd like to make a timer that flows through the scenes (time going up), how can I do this?
this is my current code (see image)
how would I be able to make the timer continue from the first level to the last while switching scenes and reloading scenes when the player dies? Thanks I'm also pretty new so forgive me if I don't really understand what you're trying to explain

valid ibex
#

The way you can do this is either having a scene that is the parent/root of the scenes that you are adding and removing to. This scene will hold this script, this means as long as the scene lives then it wont stop running, or you can make it an autoload/singleton
Example:
Timer Scene <- Timer Script
-World Manager <- where you add/remove scenes

You can read about Autoloads here: https://docs.godotengine.org/en/stable/tutorials/scripting/singletons_autoload.html
It even mentions the things I talk about, hopefully this helps with having the script run when changing scenes

#

also I would move the function to the timer function instead of process since you don't need process for this code

#

also timer shouldn't be the main scene, but this just for example

mossy island
#

thanks a lot, so if i understand correctly, I should put all of the scenes / levels into one parent scene to hold them?

#

and if i do do it like that, switchin scenes wouldn't work right? because they're all in one scene and turned into nodes?

topaz mural
#

autoloads don't get destroyed when you change a scene. You can just add the timer script (or scene) to the autoloads. This way you don't need some parent scene / ... and you can switch the scenes as usual, but the timer continues running

#

let me also suggest that instead of using a Timer node, you can also use methods like
Time.get_ticks_msec() if you want to have a more clean / stable solution.
You could then assign get_ticks_msec() to a variable when you start the timer, and compute the difference everytime you want to know the current elapsed time.

example:

var startTime

func _ready():
    startTime = Time.get_ticks_msec()

func _process(delta):
    var elapsedTime = Time.get_ticks_msec() - startTime
    # do something with it, e.g. update_ui(elapsedTime)
mossy island
#

Thank you, I have an auto load scene for the timer connected to the label, but when i place it in another scene and start the timer it still resets, i must be doing it wrong, i'll show you what i've done if that's okay. If i don't fix it soon i'll likely try and redo it the way you suggested

#

this scene is an autoload

#

and this is the world / level scene, the timer is put in but still resets when the scene reloads

topaz mural
#

Autoload means that the Node gets placed outside the currently loaded scene.

This means you dont need it in your world1 scene too.

Seems like you have two Timer Labels and probably the autoloaded one is not Shown

Suggestion: make two scripts, one for the Timer itself, one for the Label.
The Timer goes in the Autoload, the Label in the World1 scene.

Within the Label, update its text by calling the autoloaded Timer Script. You can directly access it via the Name chosen for it in the autoload

mossy island
#

okay, this is the auto load timer script, but I don't think the timer starts correctly am I doing anything wrong?

mossy island
#

hey everyone, just an update I linked everything up and the timer works connecting to the label, but there's this really weird issue happening. I put the timeout on 0.01 but wanted to change it to 0.06, however. The timer freezes if i put the timeout to 0.06, i've looked at the other numbers and it seems that with the timeout, anything larger than 0.01 just simply does not work

#

this is the timer script

#

this is the label script

topaz mural
#

I guess you never set TimerStart to false again, so it might be restarting the timer the whole time and never emitting _on_timeout()

#

but not sure if thats the problem, if you don't even set the TimerStart to true

mossy island
#

so i should add an "else" to after the "if TimerStart == true"?

topaz mural
#

did you try debugging and setting a breakpoint to line 16?

#

(and 19)

mossy island
#

oh how do i do that?

topaz mural
#

clicking on the left side of the line number, a red dot should appear

mossy island
#

ok ty

topaz mural
#

and then running the game. it should stop there and you can inspect what the variables are

mossy island
#

ok i've done it, what should I look at?

topaz mural
mossy island
#

this is what it says, sorry idk how to read any of it :((

topaz mural
#

if you press F12 it continues

#

I guess only breakpoints in line 16 and 28 are interesting here

#

does it ever reach the "_on_timeout()" method?

mossy island
#

yeah it does

#

I've played just the timer scene

#

and the debug states all the ms correctly

topaz mural
#

so the timer itself is working, although it somehow prints the ms twice

mossy island
#

yeah.. thats odd

topaz mural
#

so what is not working?

mossy island
#

well, the label connected to it is just not working

#

no actually

topaz mural
#

do you have two scripts?

mossy island
#

yeah

topaz mural
#

like two instances of timer

mossy island
#

well when i run the world, the timer doesn't work well

mossy island
#

oh huh i fixed it lmao

#

this was just = true

#

not == true

#

it's a little weird though, the timer doesn't start until the player moves

#

even though it's in a ready function of the world

topaz mural
#

can you add a _ready method in your timer script like this:

 func _ready():
    print("timer path: ",self.get_path())
#

and then check the output

mossy island
#

okay, i also jus realized that the timer isn't actually being started from the world, it's being started from the global script, from my "jump" input, which is later going to be removed, i just had it there to trigger the timer easily in the timer scene

topaz mural
# mossy island this was just = true

you also definitely need a statement within the if-clause
if TimerStart == true:
to set the timer back to false
TimerStart = false
to make sure the statement is only run once

mossy island
#

oh you're right, thanks

#

okay I think I fixed it, is it okay if I don't mark this chat as solved for a little bit just if some issues come up?

mossy island
#

okay one last thing, the timer can start in the home screen, do you know the code that would detect if it was in a levl world?

#

something like

#

if scene == "level 1":

topaz mural
#

you could do this:

get_tree().root.get_node_or_null("level 1") != null ```
#

but I guess thats not a good way to do it.

probably better to e.g. have your level call a function on the timer

  TimerTest.set_is_level(true)
mossy island
#

you are a genius