How to correctly identify node paths to child nodes of instanced scenes?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By ABridgewa

I am currently trying to use the “get_node” method to identify a path to the first of many child nodes (which are also instanced nodes) within a node that is an instanced scene from my main scene. Here is an example of my tree:

Main
--Sprite
--Inventory(Instanced Scene, location of script in question)
       --Control
             --Item1(Instanced Scene)
             --Item2(Instanced Scene)
             --Item3(Instanced Scene)

Now in my script located on the inventory node I attempt to locate Item1 by using

while i <  inv_keys.size():
    currNode = get_node("Control").get_child(i) <--error
    currNode.get_child(0).set_text(masterList[currItem][0])
    i = i+1

(inv_keys is an array of size 2 here lets say…less than number of instanced item scenes I currently have)

I keep getting an error "Attempt to call function ‘get_child’ in base ‘null instance’ on a ‘null instance’ which I have since determined indicates I am trying to call get_child on a node that does not exist.

I have tried all different ways to path to this first node including referencing all the way back to the root node and then without looping and continue to get this error. Is there another way I am supposed to identify this/these nodes (because they are instanced?)

Any help would be very much appreciated, thank you!

:bust_in_silhouette: Reply From: lowpolygon

You can right click on the node in the File System Window and a pop up menu will show. In it there is a copy path option. in you case it would be
get_node(“/root/Main/Inventory/Control”)
or you can simply use $ which should bring up your child item which is Control

This worked perfectly. However, in the past I did try to “copy node path” and it simply gave me “/Control” as the path which did not work. I wonder why it doesn’t recognize it unless I specify the path from the root node. At one point I attempted to write the path as “/root/Inventory/Control” but that did not work. I think I did not realize that the tree parent is not in fact the root node and I needed to include root and Main in the path.

This was very helpful, thank you!

ABridgewa | 2019-06-05 21:16

:bust_in_silhouette: Reply From: eons

Look at the state of your variables and scene when you get the error, is highly probable that your index is bigger than the number of children, so you are getting null for that get_node.

the while condition there may need to be
while i < inv_keys.size && i < your_control.get_child_count()

the debugger will give you more information, there seems to be some inconsistence in the child count and your inventory (maybe things are not being removed correctly in your inventory array)

Thank you for your answer. I suspected that might be the case at one point so I replaced the array I was using in the loop with a dummy array that I knew for certain had fewer indices, so that wasn’t the problem. I have the correct node path now, thank you!

ABridgewa | 2019-06-05 21:17