Wait a second in GDScript

Pause a function without blocking the game, the Godot 4 way.

SNP 142 #5B3 Filed godot gamedev

gdscript
func show_hit() -> void:
    sprite.modulate = Color.RED
    await get_tree().create_timer(0.2).timeout
    sprite.modulate = Color.WHITE

await suspends just this function; the rest of the game keeps running. Godot 3's yield(get_tree().create_timer(0.2), "timeout") is this in Godot 4.

Waiting for the next frame:

gdscript
await get_tree().process_frame

Or for a signal, like an animation finishing:

gdscript
await animation_player.animation_finished

Mind that if the node is freed while it's waiting, the line after await still runs. Check is_instance_valid(sprite) after long waits.