Skip to content
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

Add dump() and dump_string() global scope methods #77290

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Calinou
Copy link
Member

@Calinou Calinou commented May 20, 2023

dump() prints a value with formatting and additional information while returning the value. This makes for more convenient debugging.

dump_string() returns the formatted string as BBCode, which can be displayed in a RichTextLabel node or with a print_rich() statement.

In release builds, dump() acts like print() and dump_string() acts like str().

A non-rich dump_string() variant could be added for display in Label and Label3D, but this
is best done with #78310 as to avoid code duplication.

Testing project: test_dump.zip

Preview

RichTextLabel

Screenshot_20230715_210932

Terminal output (VS Code)

  • Note: Truecolor ANSI code conversion is not implemented in terminal output yet. This issue is independent of this PR.

Screenshot_20230715_211003

Editor Output panel

Screenshot_20230715_210948

@RandomShaper
Copy link
Member

I find it a bit inconsistent that both functions have 'dump' in their name while they behave so differently. Maybe dump_string() could be renamed to something like format_rich()?

Aside, in order not to bloat the core, we could consider making these trivial in non-debug builds.

@Calinou Calinou force-pushed the add-gdscript-dump-method branch from 9ac4a8b to db21777 Compare July 15, 2023 19:10
@Calinou
Copy link
Member Author

Calinou commented Jul 15, 2023

I've updated this PR with the following improvements:

  • Explicit float display now works for vector types. However, precision errors are also made explicit in the process. print() does not make precision errors explicit (try print(123.001) or print(0.2 + 0.1)), but dump() does for some reason. This may or may not be a good idea 🙂
  • Arrays, packed arrays and dictionaries now display their size at the beginning.
  • Arrays and packed arrays display the index of each item, similar to PHP's var_dump().
  • Colors are now used in arrays and dictionaries.

I find it a bit inconsistent that both functions have 'dump' in their name while they behave so differently. Maybe dump_string() could be renamed to something like format_rich()?

That makes sense, especially if we add a non-rich format() equivalent (not to be confused with String.format(), though).

Aside, in order not to bloat the core, we could consider making these trivial in non-debug builds.

This PR surprisingly appears to have a significant impact on binary size (about 50 KB on a stripped Linux editor build without LTO). I wonder if I'm doing something wrong and this impact could be reduced on debug builds. I guess putting more code in a template function doesn't help 🙁

I guess we could redirect dump() and dump_string() to act like regular print() in release builds (with dump() still returning the value it's evaluating).

Edit: Disregard the accidental close/reopen below.

@Calinou Calinou closed this Jul 15, 2023
@Calinou Calinou reopened this Jul 15, 2023
@Calinou Calinou force-pushed the add-gdscript-dump-method branch from db21777 to cfab6a1 Compare August 18, 2023 10:19
@kevinloustau
Copy link

Hey, what is missing for this PR?
I found difficult to work with the monocolor of the godot console.
Adding colors, along with a pretty print, would greatly enhance usability. 🙃

@Calinou Calinou force-pushed the add-gdscript-dump-method branch from cfab6a1 to bb4f5c4 Compare January 24, 2024 19:15
@Calinou
Copy link
Member Author

Calinou commented Jan 24, 2024

I've pushed changes that make dump() act like print() (but returning the value it's evaluating) and dump_string() act like str() in release builds to decrease the binary size of release export templates.

Binary size for the Linux editor (with optimize=speed lto=full) grew by 36 KB and 8 KB for Linux release export template (also with optimize=speed lto=full).

`dump()` prints a value with formatting and additional information
while returning the value. This makes for more convenient debugging.

`dump_string()` returns the formatted string as BBCode,
which can be displayed in a RichTextLabel node or with a
`print_rich()` statement.

In release builds, `dump()` acts like `print()` and `dump_string()`
acts like `str()`.
@Calinou Calinou force-pushed the add-gdscript-dump-method branch from bb4f5c4 to fe1c4bb Compare January 24, 2024 19:53
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.

3 participants