How to bind a c++ virtual method to gdscript

Edit 4.3.x

Since 4.3.x you can create your virtual methods. These are the steps:

In the header file, include those headers:

#include <godot_cpp/core/binder_common.hpp>
#include <godot_cpp/core/gdvirtual.gen.inc>

Now define you virtual method in the class’ declaration. Note, there are many macros to do it, example:

/// if the macro ends with the "RC" suffix, the virtual method will returns a value, 
/// which is the first argument of the macro, followed by the virtual name method 
/// and the arguments expected by the macro name itself
GDVIRTUAL1RC(float, _my_virtual_method_name, Array);
/// if the macro does not end with that suffix, then the first
GDVIRTUAL2(_another_virtual_method_name, Array, double);

Then into your implementation, inside the static method _bind_methods, use these macros

GDVIRTUAL_BIND(_my_virtual_method_name, "argument_0_name")
GDVIRTUAL_BIND(_another_virtual_method_name, "argument_0_name", "argument_1_name")

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

Edit 4.3.x

Since 4.3.x you can create your virtual methods. These are the steps:

In the header file, include those headers:

#include <godot_cpp/core/binder_common.hpp>
#include <godot_cpp/core/gdvirtual.gen.inc>

Now define you virtual method in the class’ declaration. Note, there are many macros to do it, example:

/// if the macro ends with the "RC" suffix, the virtual method will returns a value, 
/// which is the first argument of the macro, followed by the virtual name method 
/// and the arguments expected by the macro name itself
GDVIRTUAL1RC(float, _my_virtual_method_name, Array);
/// if the macro does not end with that suffix, then the first
GDVIRTUAL2(_another_virtual_method_name, Array, double);

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.