Nonexistent function error at child node

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By hansolo
:warning: Old Version Published before Godot 3 was released.

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.

At the script of generator I have:

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

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?

Could you please provide the other script as well?

Dr_TwiSteD | 2017-09-07 10:37

:bust_in_silhouette: Reply From: hansolo

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.

:bust_in_silhouette: Reply From: Dr_TwiSteD

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.

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.

hansolo | 2017-09-08 06:58

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!

Dr_TwiSteD | 2017-09-08 07:04