0 votes

I want to change the scene after a level is finished. But i need a delay of 2 seconds. how can i do that? m guessing timer or tween? any suggestions?

in Engine by (73 points)

1 Answer

0 votes

Here is a bit of code that works for setting up and using a one shot timer.
(I used it on a label just to test it out, but you might put it in a script that you assigned to your top level scene node or something.)
In the on_timeout function, you could change the scene.

extends Label

var timer = null

func _ready():
    timer = Timer.new()
    timer.set_one_shot(true)
    timer.set_timer_process_mode(Timer.TIMER_PROCESS_FIXED)
    timer.set_wait_time(5)
    timer.connect("timeout", self, "on_timeout")
    timer.start()
    add_child(timer)

func on_timeout():
    self.set_text("timeout")
    remove_child(timer)
    timer.queue_free()

func on_user_message(message):
    self.set_text(message)

For some reason, timers don't seem to work unless you actually add them to the tree. I just started with Godot myself, so I'm not sure yet about when what things get released and what happens to variables that are pointed at them (which aren't "references"?) when they do. Someone wrote that if you don't add a freshly created timer node to the tree, it gets released at the end of the function that created it (regardless of whether it was assigned to a member variable or not) because there's no reference to it - but that would imply that assigning an object to a member variable doesn't add a reference to that object. (I have verified that simply assigning it to a member variable doesn't cause it to start working.) Which might be true, but that would have a bunch of implications and raise a lot of questions. GDScript is sure not Python. Which I suppose is both good and bad.

At the moment, I'm guessing that the tree propagates behind-the-scenes messages/calls which result in things like timers doing their work, which is why timers don't work if they're not added to the tree. I'm not yet sure about whether or not member variable assignment adds a reference. Certainly data structures hang around while any member variable references to them remain? And IO handles and things like that? So..
Nah, I bet adding a reference adds a reference. Because I bet you could store a reference to a freshly created Node in a member variable, and then in a timeout handler successfully add that thing to the tree. Which couldn't happen if it were gone by the time the timer fired, so.

After calling queue_free should this code set self.timer = null? Probably so? Would that be done automatically? Probably not? Would doing it interfere with the engine's ability to release the thing? Hopefully not? Yeah I still need to do a bunch of poking about. I need to find a way to see how many references something currently has...

by (44 points)
edited by

The timer and connection can be set on the scene editor too, then just start the timer when needed.

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 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 webmaster@godotengine.org with your username.