godotengine / godot-cpp

C++ bindings for the Godot script API
MIT License
1.69k stars 527 forks source link

Node added with add_child inside GDExtension class does not get _process called #1468

Closed TvdSmagt closed 4 months ago

TvdSmagt commented 4 months ago

Godot version

4.2.2-stable_linux

godot-cpp version

4.2

System information

Linux Ubuntu 22.04

Issue description

Given a GDExtension class Parent that creates a GDExtension class Child and adds it as a child, this child node will not get its _process function called, even though it is defined and the node is present in the tree.

Steps to reproduce

Given the Parent and Child classes as defined below.

Add Parent and Child class attached to register_types and build.

ClassDB::register_class<Parent>();
ClassDB::register_class<Child>();

When running the scene it will report:

Parent::Parent
Child::Child

Whereas I would expect it to start spamming:

Child::_process
Child::_process
Child::_process
Child::_process

Minimal reproduction project

class Child : public Node {
    GDCLASS(Child, Node)

public:
    Child() {
        UtilityFunctions::print("Child::Child");
    }

    void _process(double delta) override {
        UtilityFunctions::print("Child::_process");
    };

protected:
    static void _bind_methods() {
    };
};

class Parent : public Node {
    GDCLASS(Parent, Node)
    Parent() {
        UtilityFunctions::print("Parent::Parent");
        auto child = std::make_shared<Child>();
        add_child(child.get());
    }

protected:
    static void _bind_methods() {
    };
};
TvdSmagt commented 4 months ago

Ah, I missed the edited comment of @AThousandShips in the other issue.

Using:

auto child = memnew(Child);
add_child(child);

Does make the child node call _process.