- Edited
I am confused when gettree() should be used and what are the best practices for using gettree().
Can it be used instead of
@onready var camera = $"/root/World/Camera2D"
Godot version 4
I am confused when gettree() should be used and what are the best practices for using gettree().
Can it be used instead of
@onready var camera = $"/root/World/Camera2D"
Godot version 4
tsalexey544 You use it whenever you need access to the SceneTree
singleton. It can be used in your above example, that is when you need to get to a specific node starting from the root. However using node path shorthand syntax is much more readable than cascading get_node()
calls. But technically you could do:
@onready var camera = get_tree().get_root().get_node("World").get_node("Camera2D")
Note that SceneTree
has a lot more functionality than just getting to nodes. It basically represents game's main loop. Check documentation class reference on SceneTree to see what else you can do with it.