Await a Timer
Part of the Game Rules section of Coddy's Godot journey. Lesson 5 of 29.
To wait once inside a function, you do not need a Timer node. get_tree().create_timer(2.0) makes a one shot timer in code that is already running. await pauses your function until its timeout signal arrives:
func _ready():
print("Wait for it")
await get_tree().create_timer(2.0).timeout
print("Now!")The first line prints at once. The function stops at await while the game keeps running. Two seconds later, the function carries on.
Keep .timeout at the end. await waits for a signal, and timeout is the signal.
Challenge
EasyOpen the gate two seconds after the game starts.
- In
_ready(), find the line that moves$Gate. - Above it,
awaitthetimeoutof a2.0second timer fromget_tree().create_timer().
Press Play: the gate waits two seconds, then opens. Then Check.
Try it yourself
extends Node2D
func _ready():
$Gate.position.y -= 140
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Game Rules
Practice on your own: Godot playground