Onready
Part of the Nodes and Scenes section of Coddy's Godot journey. Lesson 21 of 27.
Writing $Face/Eyes on every frame looks for the node 60 times a second. It is better to find it once and keep it in a variable.
Variables at the top of a script are made when the node is made. Its children are not attached yet, so $Face/Eyes would find nothing, and Godot will not start the game. Put @onready in front, and Godot fills the variable when the node is ready, just before _ready() runs:
@onready var mouth = $Face/Mouth
func _ready():
mouth.position.y += 2After that, mouth is the node in every function of the script.
Challenge
EasyMake a dizzy robot:
- Keep the eyes in a variable:
@onready var eyes = $Face/Eyes. - In
_process(), spineyesclockwise, half a turn every second. The eyes circle around the middle of the face, and the robot stays still.
Try it yourself
extends Node2D
func _process(delta):
pass
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Nodes and Scenes
Practice on your own: Godot playground