Wait()-like function?!

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

So, is there a built-in function to GDScript where I can pause a process in a script for a few seconds, say:

wait(1) #waits for 1 second
set_visible(true) #then continues the rest of the script after the 1 second wait
:bust_in_silhouette: Reply From: Akien

See OS.delay_msec and OS.delay_usec.

Note that if you use those in the main thread, those will cause the project to freeze until the delay has expired.

Calinou | 2016-08-17 07:05

:bust_in_silhouette: Reply From: twinpixel


var t = Timer.new()
yield(t, "timeout")

Don’t forget to free the timer afterwards, or you’ll cause a memory leak.


Bauxite | 2017-08-18 09:39

this works, but i tried to turn it into a function which didn’t
anyone knows why?

func wait(s):
var t = Timer.new()
yield(t, “timeout”)

func _ready():

it took me legit 10 minute to create an account to ask this because u have to click on send me a confirmation mail. that’s so unnecessary so here are some random chinese letters 阿贵冉宋日国

TRISTANfH | 2020-01-31 17:58

Weirdly enough, when I did this in it’s own function, it didn’t work, but when I had the exact same code on the place where I called the wait function, it worked.

The Seahorse | 2020-05-26 18:46

This is not the best answer, since Godot has it’s own dedicated function for this use-case: create_timer
(which is already mentioned in the comment below

So if you Googled this issue and ended up on this thread, check the create_timer function and yield-ing it instead of creating your own Timer

skarnl | 2020-06-26 17:41


is not a better idea. It can work in some cases, but if the object that creates the timer is freed, the game will crash. In general it’s best to avoid using yields until Godot 4.0, as I believe they fixed the issue.

alwaysusa | 2020-09-03 23:13

This causes any function you put this in to return a state instead of a value because of yield

wetbadger | 2021-09-09 15:42

If you are creating a wait(s) function and then calling it in another function, use yield in the calling method and wait for the “completed” signal of wait method.


litelo | 2022-01-24 08:43

Thank you! I know this was answered a long time ago but got me going!

landsurveying | 2022-12-21 08:44

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

Does the timer created here get disposed?

Bob Dolan | 2018-12-05 22:26

I believe that, once the routine has been resumed, the SceneTreeTimer object, returned by the create_timer () function, becomes invalid, as documented at:
GDScript — Godot Engine (3.0) documentation in English

ttkhila | 2018-12-10 14:55

this is causing my function to return

wetbadger | 2022-07-22 18:34

:bust_in_silhouette: Reply From: boston2029

yield(get_tree().create_timer(1), "timeout").

That should yield the script for the specified amount of seconds (in this case, 1.)

:bust_in_silhouette: Reply From: BlackDragonBE

In Godot 4:


func some_function():
    await get_tree().create_timer(1.0).timeout


public async void SomeFunction()
    await ToSignal(GetTree().CreateTimer(1.0f), "timeout");

thanks you so much ! this help me a lot in my current project

Didoufer | 2023-05-20 14:23