Is there a wait function to godot

Im trying to find a wait function where the script just pauses for a set amount of time

Use await get_tree().create_timer(1.0).timeout. It will wait for the time you set in the create_timer method to go to next script line.

6 Likes

I usually define my own utility function to do this:

func wait(seconds: float) -> void:
  await get_tree().create_timer(seconds).timeout
7 Likes

If this is godot 3, then I think you have to use yield(get_tree().create_timer(1.0, “timeout”) or something similar to what the other comments say; I don’t quite remember what parameters create_timer has. But I am pretty sure that in Godot 3 you use yield and in Godot 4 you use await

1 Like

This is quite clever, I think I’ll experiment that

Why cant we just have a wait() function and the number of seconds in the brackets this is unnecessary

1 Like

We absolutely can! You can follow this workflow to submit a pull request with this addition to the engine: Pull request workflow — Godot Engine (stable) documentation in English

2 Likes

I meant built in

If you use C# you can use Tasks API.

Task.Delay(TimeSpan.FromSeconds(2f)).ContinueWith(_ =>
{
    // executed after 2 seconds
});

2 Likes

Using and learning gdscripts, thank you tho

We do have this for milliseconds:

OS.delay_msec(3000)

This will do the exact same as pythons time.sleep(3) function.
HOWEVER: This will literally wait. The reason why recommend to use the async await method is that it does not halt the whole process. So yes, we do have a simple sleep function. And just like in python it is useful in some cases, not all.

Basically:

Python GDScript
time.sleep(seconds) OS.delay_msec(milliseconds)
await asyncio.sleep(seconds) await get_tree().create_timer(seconds).timeout

So it’s really not that different.

yes, I figured, I was just being a smart ass

So wait i just do

func wait(seconds: float):
OS.delay_msec(seconds * 1000)

And then whenever i want i just do “wait(30)” which will wait 30 seconds

You can do that if you really want to halt the whole game process for that time. This literally does the same as sleeping, so nothing happens during this time. No frames are rendered, no other scripts are processed, nothing moves.

Usually you do not want to sleep like this. Usually your intention is not “waiting”, but instead “delay an action”. In which case you really should use await as it is asynchronous and does exactly that: it delays the code until an event happens. In this case a timeout from a scene tree timer.

What about yield? I am on godot 3.something and theres no await command

Oh I didn’t see that. Please always specify the exact version in your question too (not only as a tag).
Then the code must use yield like this:

yield(get_tree().create_timer(seconds), "timeout")
extends Node

var time = 10
var boolean := true

func _ready():
	if time >= 10:
		if boolean:
			boolean = false
			time += 10
			print(time)
			wait(1.0)
			boolean = true
			
		

func wait(seconds: float):
	yield(get_tree().create_timer(seconds), "timeout")

i tried this script and it only printed once, did i do something wrong?

printing once is correct.

The code prints, then waits 1 second, then sets boolean to true.

There’s no command to print again after “wait” call. The ready function doesn’t get called from the beginning after the wait, it continues from where it left off.

Worth noting that no other objects wait, it’s just this specific method where you ask to wait. This method, or rather this branch of the call stack, waits. Other objects can call other methods on this object during this “wait” period no problem.

If instead you want to stop the engine from processing the scene (i.e. not call _process or _physics_process), then you need to change the scene’s Process property.

i tried to loop the script

extends Node

var time = 10
var boolean := true

func _ready():
	if time >= 10:
		while boolean:
			boolean = false
			time += 10
			print(time)
			wait(1)
			boolean = true
	

func wait(seconds: float):
	yield(get_tree().create_timer(seconds), "timeout")

it crashed, how would i prevent this