X Tutup
Skip to content

Improve to_string() and add it to Resource#94047

Merged
Repiteo merged 1 commit intogodotengine:masterfrom
KoBeWi:resource_printer
Oct 16, 2025
Merged

Improve to_string() and add it to Resource#94047
Repiteo merged 1 commit intogodotengine:masterfrom
KoBeWi:resource_printer

Conversation

@KoBeWi
Copy link
Member

@KoBeWi KoBeWi commented Jul 7, 2024

Object and Node had some duplicate code in to_string() method. I unified it by renaming the virtual to_string() to _to_string(). The base method runs the repeated code and falls back to the new method.
This means that anything can now override the default to_string() in its script. tbh the code looks like something that should be handled by GDVirtual.

I also added custom _to_string() to Resource, which returns Resource Name (path):Object, e.g.

(res://icon.svg):<CompressedTexture2D#-9223369745472882567>
Cool Gradient (res://new_gradient_texture_2d.tres):<GradientTexture2D#-9223369684470925226>

It allows to identify stringified resources.

@KoBeWi KoBeWi added this to the 4.x milestone Jul 7, 2024
@KoBeWi KoBeWi requested review from a team as code owners July 7, 2024 18:22
Copy link
Member Author

@KoBeWi KoBeWi Jul 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder why this exists. No other class has a guard in this method.

Copy link
Member

@Mickeon Mickeon Jul 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In all likelihood Reduz copied and pasted it back when he wanted to make Nodes as thread-safe as possible when implementing Thread Groups.

@fire fire requested a review from a team July 7, 2024 18:39
Copy link
Member

@fire fire left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is a good idea to unify and let everything have a human readable string instead of to string fallback.

@KoBeWi KoBeWi force-pushed the resource_printer branch 2 times, most recently from 44f8367 to a828dc9 Compare July 7, 2024 19:19
@Mickeon
Copy link
Member

Mickeon commented Jul 8, 2024

Always have been iffy on the overly verbose numerical value appended to Object. So seeing this overly extended result for _to_string does not exactly put me at ease, but it's better than nothing I suppose?

@dsnopek
Copy link
Contributor

dsnopek commented Oct 4, 2025

Thanks!

This PR would also help to solve a problem in GDExtension, where if you make a class that descends from any class that has a to_string() (which isn't Node or Object), then the _to_string() defined in the GDExtension won't be called. (This was raised on #110993.)

This needs a rebase, but otherwise looks good to me!

@KoBeWi KoBeWi force-pushed the resource_printer branch 2 times, most recently from f418344 to a3b5c9f Compare October 4, 2025 20:43
@dsnopek

This comment was marked as resolved.

@KoBeWi KoBeWi requested a review from a team as a code owner October 8, 2025 22:54
Copy link
Member

@Alex2782 Alex2782 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The changes in the platform/android/ files look OK to me; shouldn't be critical (not tested).

Copy link
Contributor

@dsnopek dsnopek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! This looks good to me :-)

Copy link
Member

@Ivorforce Ivorforce left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense to me. This matches other virtual implementations that scripts and extensions can override.

I think what @dsnopek said should also apply to scripts; this should allow scripts to override to_string(), even when they don't inherit from Object or Node.

The only change in behavior I'm seeing is that Node::to_string will no longer use a thread guard until the call is deferred to Node::_to_string. However, I don't think this is a huge problem since it's mostly a guard to protect the user from themselves. But let me know if you disagree!

@Repiteo Repiteo modified the milestones: 4.x, 4.6 Oct 16, 2025
@Repiteo Repiteo merged commit 60710df into godotengine:master Oct 16, 2025
20 checks passed
@Repiteo
Copy link
Contributor

Repiteo commented Oct 16, 2025

Thanks!

@KoBeWi KoBeWi deleted the resource_printer branch October 16, 2025 18:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants

X Tutup