0 votes

I have two nodes, one is the child of another. I want to get the child node at parent's script and call one of the child's functions.

Scene hierarchy

At the script of generator I have:

func _process(delta):
  var exp_node = get_node("expand_node")

expand() function is defined and doesn't require any arguments.

However, when I run this scene, the code runs until exp_node.expand() and gives this error at that point:

Invalid call. Nonexistent function 'expand' in base 'Node2D'.

When I inspect the exp_node in debugger I see that it is a Node2D object, without any scripts attached.

How can I call a script of the child node?

asked Sep 7, 2017 in Engine by hansolo (30 points)

Could you please provide the other script as well?

2 Answers

+1 vote
Best answer

Ok, I found the problem. I had an _init() function defined in expand_node's script. I guess this has somehow broken Node-Script compatibility.

When I removed the _init() definition, it started to work as expected. Anyhow, I would be glad if someone can explain why it happens.

answered Sep 7, 2017 by hansolo (30 points)
selected Sep 8, 2017 by hansolo
+1 vote

I don't know what you had going on there in your _init() method.
But I would strongly recommend you not to use _init() for your nodes (if not only a specific case). The _ready() method is there for the purpose.
It's because _init() is the constructor callback which may intervene with some internal stuff.
And _ready() is called right after the node is hooked up to the tree and ready to go.

answered Sep 7, 2017 by Dr_TwiSteD (170 points)
edited Sep 8, 2017 by Dr_TwiSteD

This node will be created (instantiated) often by its parent. I thought this way I can set its parameters compactly during construction. I just started using this engie, so I didn't know that it would break it.

You shouldn't have had broken it though. I've tried to get a similar setup in the test project with custom _init() callback on the child node, and everything went well, so don't quiet get what is exactly wrong with your _init().

If you have nothing but simple params initialization, then _read() is a recommended and default way to go.

Good Luck with GodotEngine!

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 How to use this Q&A? before posting your first questions.