+13 votes

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

set_visible(false)
wait(1) #waits for 1 second
set_visible(true) #then continues the rest of the script after the 1 second wait
in Engine by (31 points)
edited by

Use:
yield(gettree().createtimer(1), "timeout").

That should yield the script for the specified amount of seconds (in this case, 1.)
like my project https://godotengine.org/qa/user/kaylamarquez

func wait(s):
var t = Timer.new()
t.setwaittime(s)
t.setoneshot(true)
self.addchild(t)
t.start()
yield(t, "timeout")
t.queuefree()

func _ready():
hide()
wait(4)
show()

it took me legit trust me or check out my project: https://godotengine.org/qa/user/brendaray

func wait(s):
var t = Timer.new()
t.setwaittime(s)
t.setoneshot(true)
self.addchild(t)
t.start()
yield(t, "timeout")
t.queuefree()

func _ready():
hide()
wait(4)
show()
like my project https://godotengine.org/qa/user/joyceconley

I believe that, once the routine has been resumed, the SceneTreeTimer object, returned by the create_timer () function, becomes invalid, as documented at:
https://godotengine.org/qa/user/DiannHays

5 Answers

+23 votes
Best answer

Try:

var t = Timer.new()
t.set_wait_time(3)
t.set_one_shot(true)
self.add_child(t)
t.start()
yield(t, "timeout")
by (199 points)
selected by

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

t.queue_free()

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

func wait(s):
var t = Timer.new()
t.setwaittime(s)
t.setoneshot(true)
self.addchild(t)
t.start()
yield(t, "timeout")
t.queue
free()

func _ready():
hide()
wait(4)
show()

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 阿贵冉宋日国

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.

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

create_timer

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.

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

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.

yield(wait(2),"completed");

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

+2 votes
by (1,958 points)

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

+33 votes
yield(get_tree().create_timer(1.0), "timeout")
by (38 points)

Does the timer created here get disposed?

I believe that, once the routine has been resumed, the SceneTreeTimer object, returned by the create_timer () function, becomes invalid, as documented at:
https://docs.godotengine.org/en/3.0/getting_started/scripting/gdscript/gdscript_basics.html?highlight=yield#coroutines-with-yield

this is causing my function to return

0 votes

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

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

by (92 points)
+2 votes

In Godot 4:

GDScript:

func some_function():
    print("start")
    await get_tree().create_timer(1.0).timeout
    print("end")

C#:

public async void SomeFunction()
{
    GD.Print("start");
    await ToSignal(GetTree().CreateTimer(1.0f), "timeout");
    GD.Print("end");
}
by (18 points)

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

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.