From f38c056b674b3d9eef5d92582e880a349e2ffe7b Mon Sep 17 00:00:00 2001 From: David Snopek Date: Fri, 25 Apr 2025 14:31:10 -0500 Subject: [PATCH] Fix classes without `_to_string()` always returning `"[Wrapped:0]"` --- binding_generator.py | 10 ++++++++++ include/godot_cpp/classes/wrapped.hpp | 6 +----- test/project/main.gd | 3 +-- 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/binding_generator.py b/binding_generator.py index 8dabe578a..807aecd5b 100644 --- a/binding_generator.py +++ b/binding_generator.py @@ -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. diff --git a/include/godot_cpp/classes/wrapped.hpp b/include/godot_cpp/classes/wrapped.hpp index 2b4a9035e..647d611b1 100644 --- a/include/godot_cpp/classes/wrapped.hpp +++ b/include/godot_cpp/classes/wrapped.hpp @@ -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 ""; } 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; } @@ -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; }; diff --git a/test/project/main.gd b/test/project/main.gd index b69ad75a0..5ade71719 100644 --- a/test/project/main.gd +++ b/test/project/main.gd @@ -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:' % $Example/ExampleMin.get_instance_id()) # Call static methods. assert_equal(Example.test_static(9, 100), 109);