How can I pause a Thread for some seconds in Godot?
Asked Answered
I

5

8

How can I pause execution for a certain amount of time in Godot? I can't really find a clear answer.

Imamate answered 1/7, 2022 at 13:45 Comment(3)
Did you look into docs.godotengine.org/de/stable/tutorials/performance/threads/… ?Puttyroot
Yes, but there they don't show how to pause itImamate
Well they do. By using semaphores you can pause the thread. If you want to do it for a specific amount of time you could use an timer which posts the semaphore after an amount of time.Puttyroot
B
4

The equivalent of Thread.Sleep(1000); for Godot is OS.DelayMsec(1000). The documentation says:

Delays execution of the current thread by msec milliseconds. msec must be greater than or equal to 0. Otherwise, delay_msec will do nothing and will print an error message.

Note: delay_msec is a blocking way to delay code execution. To delay code execution in a non-blocking way, see SceneTree.create_timer. Yielding with SceneTree.create_timer will delay the execution of code placed below the yield without affecting the rest of the project (or editor, for EditorPlugins and EditorScripts).

Note: When delay_msec is called on the main thread, it will freeze the project and will prevent it from redrawing and registering input until the delay has passed. When using delay_msec as part of an EditorPlugin or EditorScript, it will freeze the editor but won't freeze the project if it is currently running (since the project is an independent child process).

Borchers answered 1/7, 2022 at 15:4 Comment(0)
S
11

The same one-liner but for Godot 4.0 would be:

await get_tree().create_timer(1).timeout
Shambles answered 19/3, 2023 at 22:25 Comment(0)
B
4

The equivalent of Thread.Sleep(1000); for Godot is OS.DelayMsec(1000). The documentation says:

Delays execution of the current thread by msec milliseconds. msec must be greater than or equal to 0. Otherwise, delay_msec will do nothing and will print an error message.

Note: delay_msec is a blocking way to delay code execution. To delay code execution in a non-blocking way, see SceneTree.create_timer. Yielding with SceneTree.create_timer will delay the execution of code placed below the yield without affecting the rest of the project (or editor, for EditorPlugins and EditorScripts).

Note: When delay_msec is called on the main thread, it will freeze the project and will prevent it from redrawing and registering input until the delay has passed. When using delay_msec as part of an EditorPlugin or EditorScript, it will freeze the editor but won't freeze the project if it is currently running (since the project is an independent child process).

Borchers answered 1/7, 2022 at 15:4 Comment(0)
G
4

One-liner:

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

This will delay the execution of the following line for 1 second.

Usually I make this to a sleep() function for convenience:

func sleep(sec):
    yield(get_tree().create_timer(sec), "timeout")

Call it with sleep(1) to delay 1 second.

Glebe answered 29/10, 2022 at 11:51 Comment(0)
M
0

Godot 4 what worked for me was: await get_tree().create_timer(1).timeout

Marlborough answered 8/12, 2023 at 7:25 Comment(1)
This exact same answer has already been posted...Crossbill
W
0

I see different answers but an important aspect is not mentioned: would you like to have the thread blocked or waiting?

see related wiki page

Blocking way:

Thread.Sleep(1000);

Non-blocking:

await get_tree().create_timer(1).timeout
Whereof answered 29/2 at 9:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.