Skip to content

cast_to and virtual function confusing behavior #910

Description

@chrisculver

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions