0 votes

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:

--Inventory(Instanced Scene, location of script in question)
             --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
    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 'getchild' in base 'null instance' on a 'null instance' which I have since determined indicates I am trying to call getchild 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!

in Engine by (12 points)

2 Answers

+1 vote

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
or you can simply use $ which should bring up your child item which is Control

by (175 points)

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!

0 votes

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)

by (7,898 points)

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!

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.