Skip to content

Commit 5a16059

Browse files
authored
Merge pull request #1776 from dsnopek/fix-default-to-string
Fix classes without `_to_string()` always returning `"[Wrapped:0]"`
2 parents 08fd033 + f38c056 commit 5a16059

File tree

3 files changed

+12
-7
lines changed

3 files changed

+12
-7
lines changed

binding_generator.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1723,6 +1723,16 @@ def generate_engine_class_header(class_api, used_classes, fully_used_classes, us
17231723
result.append(f"\t~{class_name}();")
17241724
result.append("")
17251725

1726+
if class_name == "Object":
1727+
result.append('\tString _to_string() const { return "<" + get_class() + "#" + itos(get_instance_id()) + ">"; }')
1728+
result.append("")
1729+
1730+
if class_name == "Node":
1731+
result.append(
1732+
'\tString _to_string() const { return (!get_name().is_empty() ? String(get_name()) + ":" : "") + Object::_to_string(); }'
1733+
)
1734+
result.append("")
1735+
17261736
result.append("public:")
17271737

17281738
# Special cases.

include/godot_cpp/classes/wrapped.hpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ class Wrapped {
9393
bool _property_can_revert(const StringName &p_name) const { return false; }
9494
bool _property_get_revert(const StringName &p_name, Variant &r_property) const { return false; }
9595
void _validate_property(PropertyInfo &p_property) const {}
96-
String _to_string() const { return "[" + String(get_class_static()) + ":" + itos(get_instance_id()) + "]"; }
96+
String _to_string() const { return "<Wrapped#0>"; }
9797

9898
static void notification_bind(GDExtensionClassInstancePtr p_instance, int32_t p_what, GDExtensionBool p_reversed) {}
9999
static GDExtensionBool set_bind(GDExtensionClassInstancePtr p_instance, GDExtensionConstStringNamePtr p_name, GDExtensionConstVariantPtr p_value) { return false; }
@@ -121,10 +121,6 @@ class Wrapped {
121121
return string_name;
122122
}
123123

124-
uint64_t get_instance_id() const {
125-
return 0;
126-
}
127-
128124
// Must be public but you should not touch this.
129125
GodotObject *_owner = nullptr;
130126
};

test/project/main.gd

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ func _ready():
1818

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

2423
# Call static methods.
2524
assert_equal(Example.test_static(9, 100), 109);

0 commit comments

Comments
 (0)