Overriding non-virtual cpp method

Godot Version

v4.4.1.stable.official.49a5bc7b6

Question

I’m trying to create a custom class extending CharacterBody3D in a GDExtension to modify some of the _move_and_slide_grounded() code.
I’m pretty new to C++ but have background in C.

While trying to override _move_and_slide_grounded(), compiler is not happy, I cannot override non-virtual methods.

It seems a bit weird to me, it is a big limitation. Do I need a custom_move_and_slide() ?
Is it really not possible to redefine part of the behavior of a parent class while still calling the functionality normally ?

I would like to call the “normal” move_and_slide() from my custom class from GDScript, either accessing my own cpp custom_move_and_slide(), either, even better, accessing the “real” cpp move_and_slide(), which would call instead of the “real” _move_and_slide_grounded(), my own method.

You could override through DLL injection/LD_PRELOAD but I highly doubt that’s supported in Godot, nor a fun development environment to use generally.

C++ functions are only overrideable because of vtables, which aren’t particularly performant so it’s best avoided where possible, especially in games. You will have to work around it sadly.

1 Like