Godot4

@onready

Valentyne 2023. 11. 24. 18:00

https://docs.godotengine.org/en/4.1/tutorials/scripting/gdscript/gdscript_basics.html#onready-annotation

https://www.reddit.com/r/godot/comments/c62psf/i_do_not_know_what_the_meaning_of_onready_do_you/

var my_label

func _ready():
	my_label = get_node("MyLabel")
    
#same as above
@onready var my_label = get_node("MyLabel")

 

 

 

standard variables are initialized when object is created, onready variables are initialized after the object is added to the scene tree.

 

Performance-wise, it's also good practice to cache get_node() calls by assigning them to a variable. Doing so in an onready member variable makes it easy to access the cached result from any function in the script.