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?

in Engine by (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.

by (30 points)
selected by
+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.

by (172 points)
edited by

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 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 [email protected] with your username.