From 533cd04d2a6f67497fc8ddd3fd2ccd78c9f58e2b Mon Sep 17 00:00:00 2001 From: Marc Gilleron Date: Tue, 20 Sep 2022 00:29:39 +0100 Subject: [PATCH] Fix deriving a custom class with virtual methods --- binding_generator.py | 11 ++++++++--- include/godot_cpp/classes/wrapped.hpp | 6 +++--- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/binding_generator.py b/binding_generator.py index 316bd7ba6e..d2e545031e 100644 --- a/binding_generator.py +++ b/binding_generator.py @@ -1071,17 +1071,22 @@ def generate_engine_class_header(class_api, used_classes, fully_used_classes, us result.append(method_signature + ";") result.append("protected:") - result.append("\ttemplate ") + # T is the custom class we want to register (from which the call initiates, going up the inheritance chain), + # B is its base class (can be a custom class too, that's why we pass it). + result.append("\ttemplate ") result.append("\tstatic void register_virtuals() {") if class_name != "Object": - result.append(f"\t\t{inherits}::register_virtuals();") + result.append(f"\t\t{inherits}::register_virtuals();") if "methods" in class_api: for method in class_api["methods"]: if not method["is_virtual"]: continue method_name = escape_identifier(method["name"]) result.append( - f"\t\tif constexpr (!std::is_same_v) {{" + # If the method is different from the base class, it means T overrides it, so it needs to be bound. + # Note that with an `if constexpr`, the code inside the `if` will not even be compiled if the + # condition returns false (in such cases it can't compile due to ambiguity). + f"\t\tif constexpr (!std::is_same_v) {{" ) result.append(f"\t\t\tBIND_VIRTUAL_METHOD(T, {method_name});") result.append("\t\t}") diff --git a/include/godot_cpp/classes/wrapped.hpp b/include/godot_cpp/classes/wrapped.hpp index 62ec0d864d..94f9f97a00 100644 --- a/include/godot_cpp/classes/wrapped.hpp +++ b/include/godot_cpp/classes/wrapped.hpp @@ -145,9 +145,9 @@ protected: return (::godot::String(::godot::Wrapped::*)()) & m_class::_to_string; \ } \ \ - template \ + template \ static void register_virtuals() { \ - m_inherits::register_virtuals(); \ + m_inherits::register_virtuals(); \ } \ \ public: \ @@ -159,7 +159,7 @@ public: m_inherits::initialize_class(); \ if (m_class::_get_bind_methods() != m_inherits::_get_bind_methods()) { \ _bind_methods(); \ - m_inherits::register_virtuals(); \ + m_inherits::register_virtuals(); \ } \ initialized = true; \ } \