Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cast_to and virtual function confusing behavior #910

Closed
chrisculver opened this issue Nov 5, 2022 · 0 comments · Fixed by #1377
Closed

cast_to and virtual function confusing behavior #910

chrisculver opened this issue Nov 5, 2022 · 0 comments · Fixed by #1377

Comments

@chrisculver
Copy link

chrisculver commented Nov 5, 2022

Summary: I ran into confusion implementing my own virtual function in a GDExtension class and overwritting it in GDScript. There may be a fix by properly binding the virtual method. For any solution an example could be added to the test/src/example.h

Minimal Test Program: Only showing the relevant part of the class, not the boilerplate for GDExtension.

// test.h
class Test : public Node 
{
    GDCLASS(Test, Node);
protected:
    static void _bind_methods();
public:
    virtual void update();
    void update_all_children();
};

// test.cpp
void Test::_bind_methods()
{
    // Abstract Methods
    ClassDB::bind_method("update", &Test::update);
    ClassDB::bind_method("update_all_children", &Test::update_all_children);
}
void Test::update()
{
    UtilityFunctions::print("Default update from node=",get_name());
}
void Test::update_all_children()
{
    for(size_t i=0; i<get_child_count(); ++i)
    {
        (Object::cast_to<Test>(get_child(i)))->update();
    }
}

Problem: If I overwrite the update method in GDScript, then future calls directly to update in GDScript call the correct update. The problem is when calling update_all_children in GDScript in which case all children will call the update you see defined in test.cpp.

Temporary Fix: Replace ->update() with `->call("update"). This produces the correct output for all children.

Solution: I think the ->update() syntax should call the overridden version in GDScript, not the cpp implementation. Perhaps this is how it works if using GDVIRTUAL0 or bind_virtual_method would. There isn't currenty an example for this, and I couldn't get it to compile on my own. Either way there isn't currently an example for defining and using your own virtual method.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
1 participant