Using add_child() in GDExtension but not getting _process() call
Asked Answered
C

5

0

Hello. I am quite new to godot so hopefully it's not something obvious.
When I use add_child() in GDScript the child is getting the _process() calls automatically in its own script.

I made a Node2d GDExtension in C++ and want to add some Childs to this class. I can add them with add_child but the classes don't get the _process() call. I did set_process(true) inside the Child; In the documentation it says: Note: This method is only called if the node is present in the scene tree (i.e. if it's not an orphan). Do I understand I right that its in the scene tree when its added as a Child to the Parent which is inside the scene? The Parent gets the _process() calls by the way.
I also tried set_owner() once even I don't really know what It does πŸ™‚

I anyone has some hints that would be helpful.
Thank you

Cottle answered 19/1 at 16:54 Comment(0)
P
0

Might need to call set_process(true)

Pleuron answered 20/1 at 2:54 Comment(0)
K
0

Have you confirmed that the nodes are in the runtime scene tree, by viewing them in the Remote tab or printing the tree?

Klecka answered 20/1 at 3:4 Comment(0)
C
0

Thanks for your answers

Pleuron
Yes I did that inside the child init and also again after I added the child with add_child(). Just to be sure πŸ™‚

Klecka
I used get_child_count() on my Parent Node and then run a for loop with:
Node *child = get_child(i); String child_name = child->get_name();
The names were correct and the Childs were there. Maybe there is a better way to print the whole tree?

After trying around for hours I maybe found the reason. Hopefully it helps someone saving some time in the future. These things were important:

  • The child Class also needs this GDCLASS(ChildNode, Node2D); in its class declarations. I thought that's just something the Parent class needs to be recognised by Godot. Then it also needs to have the static void _bind_methods().

  • When init the childClass you have to use ChildNode * childNode = memnew(ChildNode);
    ChildNode * childNode = new ChildNode(); doesn't work. Also just having ChildNode childNode; in your parent class declarations and then using add_child(&childNode); doesn't work.

  • And now the biggest part: All your child Nodes need to be in you void initialize_gdmain(ModuleInitializationLevel p_level) or however you call it in your register_types.cpp. Something like that: ClassDB::register_class< ChildNode>();

Maybe I missed this part in the GDExtension tutorial πŸ™‚ Anyway. If I can skip some parts or if someone knows why I have to use memnew() to make it work that would be interesting.

Greetings

Cottle answered 20/1 at 9:56 Comment(0)
K
0

You should submit a bug report at github. I don't know if it's a bug in the documentation or in the GDExtension code.

Klecka answered 20/1 at 15:46 Comment(0)
C
0

Klecka
From what I understand now that's just the way it has to be. memnew() does something which is needed for the class to be connected properly.

I am struggling with another issue now. Will open another post for that.

Cottle answered 21/1 at 13:50 Comment(0)

© 2022 - 2024 β€” McMap. All rights reserved.