Change constructor/destructor management of extension classes
This makes sure custom constructors are always called on extension classes. However, note that constructors should not take any parameters, since Godot doesn't support that. Parameters are ignore in memnew macro. Use memnew(MyClass()) instead of memnew(MyClass) since it now needs a value instead of a class name. This macro calls MyClass::_new() (define in GDCLASS macro) which ultimately calls Godot to create the object, ensuring that both the Godot and the extension instances are created. Non Godot classes (that don't derive godot::Object) are constructed as usual an can have parameters. memdelete is also changed for the same reason, as it needs to destroy the Godot object as well, and that automatically frees the bound extension instance.
This commit is contained in:
committed by
Bastiaan Olij
parent
e839199848
commit
38ee8bfcf7
@@ -889,6 +889,10 @@ def generate_engine_class_header(class_api, used_classes, fully_used_classes, us
|
||||
|
||||
result.append("public:")
|
||||
|
||||
# Constructor override, since parent Wrapped has protected constructor.
|
||||
result.append(f"\t{class_name}() = default;")
|
||||
result.append("")
|
||||
|
||||
if "enums" in class_api:
|
||||
for enum_api in class_api["enums"]:
|
||||
result.append(f'\tenum {enum_api["name"]} {{')
|
||||
@@ -963,10 +967,6 @@ def generate_engine_class_header(class_api, used_classes, fully_used_classes, us
|
||||
"\tT *get_node(const NodePath &p_path) const { return Object::cast_to<T>(get_node_internal(p_path)); }"
|
||||
)
|
||||
|
||||
# Constructor.
|
||||
result.append("")
|
||||
result.append(f"\t{class_name}();")
|
||||
|
||||
result.append("")
|
||||
result.append("};")
|
||||
result.append("")
|
||||
@@ -1104,17 +1104,6 @@ def generate_engine_class_source(class_api, used_classes, fully_used_classes, us
|
||||
result.append(method_signature)
|
||||
result.append("")
|
||||
|
||||
# Constructor.
|
||||
result.append(f"{class_name}::{class_name}() : {inherits}(godot::internal::empty_constructor()) {{")
|
||||
result.append(
|
||||
f'\tstatic GDNativeClassConstructor constructor = internal::interface->classdb_get_constructor("{class_name}");'
|
||||
)
|
||||
result.append("\t_owner = (GodotObject *)constructor();")
|
||||
result.append(
|
||||
f"\tinternal::interface->object_set_instance_binding((GDNativeObjectPtr)_owner, internal::token, this, &{class_name}::___binding_callbacks);"
|
||||
)
|
||||
result.append("}")
|
||||
|
||||
result.append("")
|
||||
result.append("} // namespace godot ")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user