How to bind a c++ virtual method to gdscript

Godot Version

4.2.x

Question

Hello, I am porting my extension to c++ and I need to mark several functions as virtuals in gdscript.

What I tried

I tried using the BIND_VIRTUAL_METHOD but it does not mark my method as virtual in gdscript.

If I try to create an empty method in the header file called for example _foo, then bind it in the static method _bind_methods as

BIND_VIRTUAL_METHOD(MyClass, _foo)

it does not appear in the methods listed by gdscript.

Could somebody give me a hint? Thanks in advance

1 Like

These issues are related. They all seem to mention using call instead.

Ok, so as a TLDR, it is not possible at the moment without using ugly and fragile workarounds.

Thank you!

1 Like

Yes. BIND_VIRTUAL_METHOD only seems to be used in the godot_cpp bindings and I believe it’s very different from what the engine uses to expose the methods like _ready and _process. If you’re a module developer, you may have more to work with than with GDExtension.

2024-03-21 Update

In Godot 4.3 there will be a new ClassDB method called add_virtual_method

The signature is simple:

static void godot::ClassDB::add_virtual_method(const godot::StringName &p_class, const godot::MethodInfo &p_method, const godot::Vector<godot::StringName> &p_arg_names = godot::Vector<godot::StringName>())

If you need to expose virtuals which can be called from your code, this is the way.

1 Like