Execute a function after a time delay

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Zylann
:warning: Old Version Published before Godot 3 was released.

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).

:bust_in_silhouette: Reply From: dc1a0

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)
:bust_in_silhouette: Reply From: Bojidar Marinov

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")

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

tween.interpolate_callback(self, 1, "queue_free")

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)

Zylann | 2016-03-19 13:39

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

Bojidar Marinov | 2016-03-19 14:44

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

Zylann | 2016-03-19 18:42

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

Bojidar Marinov | 2016-03-19 18:47

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 :slight_smile:

Zylann | 2016-03-19 18:54

Look this in 2022: SceneTreeTimer — Documentación de Godot Engine (4.x) en español
SceneTreeTimer. Not require the instantiation of a node.

Barrrettt | 2022-12-05 23:58

:bust_in_silhouette: Reply From: heston_in_mtl
yield(get_tree().create_timer(1.0), "timeout")

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

rannsaka | 2020-07-07 17:39

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

heston_in_mtl | 2020-07-08 14:40

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

milkiscool | 2020-08-01 22:09

you are a lifesaver

amberlimshin | 2021-01-24 03:10

:bust_in_silhouette: Reply From: MaxCWam

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():
yield(get_tree().create_timer(1.0), "timeout")