Image not available
How to access nodes in GDScript - Godot 4
Published: May 29, 2024
Last updated: June 4, 2024
- Code snippet
Here are the different ways to access nodes in GDScript:
# if 'ChildNode' is a child of the node this script is attached to
@onready var child_node=get_node("ChildNode")
# this is the shorthand notation for `get_node`
@onready var child_node_shorthand=$ChildNode
# to access a parent of the node this script is attached to
# quotation marks are needed if you use a dot or a slash in the
# path name
@onready var parent_node=$".."
# if 'SiblingNode' is a sibling of the node this script is attached to
@onready var sibling_node=$"../SiblingNode"
# Paths can be chained to access nodes anywhere in the tree
@onready var far_away_node=$"../../Node1/Node2/Node3/FarAwayNode"
# if a node in the tree has "Access as unique name" enabled,
# the following will work regardless of where the node is
@onready var unique_far_away_node=%FarAwayNode
To enable "Access as unique name" for a node, right click on it in the inspector and select "Access as unique name". A percentage sign will appear next to it in the inspector.
About the author
Hey there 👋
I'm Blaubessen, a web dev learning Godot. This website is my way of documenting my learnings, and hopefully be useful to other folks as well.
If you find mistakes, disagree with something written, or just have general feedback please feel free to leave a comment on this form!