Menu
CoddyTech

Onready

Part of the Nodes and Scenes section of Coddy's Godot journey. Lesson 21 of 28.

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 += 2

After that, mouth is the node in every function of the script.

challenge icon

Challenge

Easy

Make a dizzy robot whose eyes spin.

  1. At the top of the script, keep the eyes in a variable: @onready var eyes = $Face/Eyes.
  2. In _process(), spin eyes clockwise, half a turn (180 degrees) every second.

Press Play: the eyes circle the middle of the face, and the robot stays still. Then Check.

Try it yourself

extends Node2D

func _process(delta):
	pass

All lessons in Nodes and Scenes

Practice on your own: Godot playground