Skip to content

Fix classes without _to_string() always returning "[Wrapped:0]" #1776

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions binding_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -1723,6 +1723,16 @@ def generate_engine_class_header(class_api, used_classes, fully_used_classes, us
result.append(f"\t~{class_name}();")
result.append("")

if class_name == "Object":
result.append('\tString _to_string() const { return "<" + get_class() + "#" + itos(get_instance_id()) + ">"; }')
result.append("")

if class_name == "Node":
result.append(
'\tString _to_string() const { return (!get_name().is_empty() ? String(get_name()) + ":" : "") + Object::_to_string(); }'
)
result.append("")

result.append("public:")

# Special cases.
Expand Down
6 changes: 1 addition & 5 deletions include/godot_cpp/classes/wrapped.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ class Wrapped {
bool _property_can_revert(const StringName &p_name) const { return false; }
bool _property_get_revert(const StringName &p_name, Variant &r_property) const { return false; }
void _validate_property(PropertyInfo &p_property) const {}
String _to_string() const { return "[" + String(get_class_static()) + ":" + itos(get_instance_id()) + "]"; }
String _to_string() const { return "<Wrapped#0>"; }

static void notification_bind(GDExtensionClassInstancePtr p_instance, int32_t p_what, GDExtensionBool p_reversed) {}
static GDExtensionBool set_bind(GDExtensionClassInstancePtr p_instance, GDExtensionConstStringNamePtr p_name, GDExtensionConstVariantPtr p_value) { return false; }
Expand Down Expand Up @@ -121,10 +121,6 @@ class Wrapped {
return string_name;
}

uint64_t get_instance_id() const {
return 0;
}

// Must be public but you should not touch this.
GodotObject *_owner = nullptr;
};
Expand Down
3 changes: 1 addition & 2 deletions test/project/main.gd
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ func _ready():

# To string.
assert_equal(example.to_string(),'[ GDExtension::Example <--> Instance ID:%s ]' % example.get_instance_id())
# It appears there's a bug with instance ids :-(
#assert_equal($Example/ExampleMin.to_string(), 'ExampleMin:[Wrapped:%s]' % $Example/ExampleMin.get_instance_id())
assert_equal($Example/ExampleMin.to_string(), 'ExampleMin:<ExampleMin#%s>' % $Example/ExampleMin.get_instance_id())

# Call static methods.
assert_equal(Example.test_static(9, 100), 109);
Expand Down
Loading