get_node(NodePath)
fetches a node & can use the NodePath
can be either a relative path (from the current node) or absolute. The relative path is typically shorter. When used as a literal you can further shorten the expression to $NodePath
.
So with this Player
scene:
And this PlayerGun
scene:
Then in order to access the somewhat deeply nested Muzzle
node from within my script for Player
I would do this in 3.x:
onready var muzzle = $Sprite/PlayerGun/Sprite/Muzzle
or this in 4.x:
@onready var muzzle = $Sprite/PlayerGun/Sprite/Muzzle
Because I'm using a relative path, I don't need the full / absolute path. As as a bonus, the absolute path could possible change, but as long as I don't change the relative relationship between Player
and Muzzle
nothing breaks.
get_node()
is more performant than find_node()
. find_node()
has to do a pattern matching search for its target whereas get_node()
uses a hash map to children (which is also how unique scene nodes work).