I think you can do this, but not with $
reference like that in GDScript. As /u/brainbug
posted here: https://godotengine.org/qa/43578/how-to-get-a-variable-from-a-child-node you can get the child dynamically using get_node
For example, if you have:
- MyNode (Parent Node)
-- MySprite (Child Node)
--- MyOtherNode (GrandChild Node)
-- MySprite2 (Other Child Node 2)
-- MySprite3 (Other Child Node 3)
You can use the $
reference the Child Node MySprite
with $MySprite
or the Other Child Node with $MySprite2
.
or you could use the get_node
function to get the child node. The pathing is relative so you could do it like this:
var mySprite = get_node("MySprite") # to get the Child Node
var mySprite2 = get_node("MySprite2") # to get the Other Child Node
var myGrandChild = get_node("MySprite/MyOtherNode") # to get the Grand Child Node
Now that you can do that, you can use string concatenation or interpolation to get the one you want:
var mySprite2 = get_node("MySprite" + "2") # to get the Child Node 2
var mySprite3 = get_node("MySprite" + "3") # to get the Other Child Node 3
or
var mySprite2 = get_node("MySprite{number}".format({"number":"2")) # to get the Child Node 2
String Reference: https://docs.godotengine.org/en/3.1/getting_started/scripting/gdscript/gdscript_format_string.html