I've been saving some connections in a variable that my wandering code can keep track for use elsewhere.
Normally I can use something like $thisnode/childnode/child2node etc to access a child instead of find_node('childnode').find_node('child2node').
However, when I recall one of my saved locations in a variable (ie thisnode = self) and need to access a child node, I WANT to do this: thisnode.$childnode/child2node but that doesn't work and I'm forced to do thisnode.find_child('childnode').find_child... etc.
Is there a more sugary way to find child nodes in my situation?
thanks
how to use $node syntax
- Edited
Shadowphile find_node('childnode').find_node('child2node')
get_node's argument is a NodePath, so you can use:
get_node("childnode/child2node")
See the examples here:
https://docs.godotengine.org/en/4.2/classes/class_node.html#class-node-method-get-node
You might be able to reduce the need for complex node paths by locating the code in a script that's attached to a different node.
DaveTheCoder
that's good to know, if I cant' do better.
Was hoping for some direct path like the $ syntax, without calling anything but if this is it, then I am still better off lol!
thanks
Shadowphile Was hoping for some direct path like the $ syntax, without calling anything
Keep in mind that $a/b/c
is just shorthand for get_node("a/b/c")
. Internally they are the same and the engine is still calling get_node()
so you are not optimizing that call away. The $shorthand syntax is just a little simpler to read and write.
https://docs.godotengine.org/en/4.2/tutorials/scripting/gdscript/gdscript_basics.html#literals
- Edited
You write : >...Is there a more sugary way to find child nodes in my situation?...
a totally other way to find child nodes is to store them in a list when spawning with AddChild, so you dont have to search them, cause you have them in your list:
public List<Node2D> nodelist = new List<Node2D>();
...
nodelist.Add(spawnednode));
...