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.
+14 votes

How can I execute a function after a time delay without having to create an Animation?
For example, call Node.queue_free after 2 seconds?

In Unity this is done with Invoke(callback, seconds).

in Engine by (29,510 points)
edited by

4 Answers

+12 votes
Best answer

One idea would be to make a variable called time_left, and decrement it in _fixed_process by delta. After it is less than zero, call the function you want.

Another way would be to use a Timer node, and its timeout signal:

timer.connect("timeout", self, "queue_free")
timer.set_wait_time(1)
timer.start()

A third way would be to use the Tween node, which has some quite versatile uses...

tween.interpolate_callback(self, 1, "queue_free")
tween.start()
by (1,608 points)
selected by

Ok, at the moment I use a custom timer in a the _process() method.
But because it's a bit long to write, I also thought about a manager like this:

some_global_script.execute_later(self, "my_callback", 2)

Well, you can easily set this up with the Tween approach.

Yes, but it involves the creation of a node anyways.

Since global (autoload) scripts are also nodes, it wouldn't be a big deal to addchild(mynewshinysubnode)... except that you would have to refer to them by variable, and not by name.

It's just a matter of saving a node, in case I'm going to have many instances of the scene requesting the invoke_later(). But at the moment I don't care that much, so I'll take the option requiring the less work :)

Look this in 2022: https://docs.godotengine.org/es/stable/classes/class_scenetreetimer.html
SceneTreeTimer. Not require the instantiation of a node.

+1 vote

There's a couple ways this can be done.

1) You can add a timer, and run the function when it times out.
2) you can set up a couple variables of your own to hold the time and a value to check against time out and make your own accumulator (either in frames or by accumulating delta)

by (225 points)
+26 votes
yield(get_tree().create_timer(1.0), "timeout")
by (42 points)

This is what I was looking for. Made an account just to say thank you!

Thank you for the note @rannsaka ! Glad it helped.

thanks for this answer! It has helped me fix a bug i've had in my game for a while :)

you are a lifesaver

+1 vote

Got this from the Godot docs: a comment by Heston made my game lag, I did this and I had no stuttering
Edit: it only lagged when I ran it in the process function, creating the timer in the process function using this resulted in no lag.

func some_function():
print("start")
yield(get_tree().create_timer(1.0), "timeout")
print("end")
by (16 points)
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.