Skip to content

GDScript: Update get_stack(), print_stack(), and print_debug() #105801

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

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

Conversation

dalexeev
Copy link
Member

@dalexeev dalexeev commented Apr 26, 2025

Initially I suggested deprecating these functions, but now I think it makes sense to have shorthands for simple scenarios. We just need to add a reference to Engine.capture_script_backtraces() for advanced scenarios.

  • Remove the restriction on using these functions only in the main thread. This already works for Engine.capture_script_backtraces(), since the stack information is stored in thread_local variables.
  • Update documentation. These functions and Engine.capture_script_backtraces() use the same mechanism, so the same rules apply to them.
  • Add stack output to Utils.check(), see the comment.
@tool
extends EditorScript

func _run() -> void:
    var thread := Thread.new()
    thread.start(_thread_function)
    test()
    thread.wait_to_finish()

func _thread_function() -> void:
    test()

func test() -> void:
    #print_stack()
    #print(Engine.capture_script_backtraces())
    for i in 10:
        prints(i, get_stack().slice(1))

[b]Note:[/b] This function only works if the running instance is connected to a debugging server (i.e. an editor instance). [method get_stack] will not work in projects exported in release mode, or in projects exported in debug mode if not connected to a debugging server.
[b]Note:[/b] Calling this function from a [Thread] is not supported. Doing so will return an empty array.
See also [method print_debug], [method print_stack], and [method Engine.capture_script_backtraces].
[b]Note:[/b] By default, backtraces are only available in editor builds and debug builds. To enable them for release builds as well, you need to enable [member ProjectSettings.debug/settings/gdscript/always_track_call_stacks].
Copy link
Contributor

Choose a reason for hiding this comment

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

I know what you mean, but maybe it's worth being just a bit more specific, and maybe slightly less technical?
This makes me notice how rarely the three terms are used. "debug build", "editor build", "release build". I've more often seen "when running the project in [...]" and similar (basically expanding the meaning of a "build"), but it's harder to keep count of them.

Suggested change
[b]Note:[/b] By default, backtraces are only available in editor builds and debug builds. To enable them for release builds as well, you need to enable [member ProjectSettings.debug/settings/gdscript/always_track_call_stacks].
[b]Note:[/b] By default, this function is only available when running the project in the editor or in debug builds. To enable them for release builds, [member ProjectSettings.debug/settings/gdscript/always_track_call_stacks] must be enabled.

Mmmm...
Also applies for the other descriptions.

Copy link
Member Author

Choose a reason for hiding this comment

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

Borrowed from here:

<method name="capture_script_backtraces" qualifiers="const">
<return type="ScriptBacktrace[]" />
<param index="0" name="include_variables" type="bool" default="false" />
<description>
Captures and returns backtraces from all registered script languages.
By default, the returned [ScriptBacktrace] will only contain stack frames in editor builds and debug builds. To enable them for release builds as well, you need to enable [member ProjectSettings.debug/settings/gdscript/always_track_call_stacks].
If [param include_variables] is [code]true[/code], the backtrace will also include the names and values of any global variables (e.g. autoload singletons) at the point of the capture, as well as local variables and class member variables at each stack frame. This will however will only be respected when running the game with a debugger attached, like when running the game from the editor. To enable it for export builds as well, you need to enable [member ProjectSettings.debug/settings/gdscript/always_track_local_variables].
[b]Warning:[/b] When [param include_variables] is [code]true[/code], any captured variables can potentially (e.g. with GDScript backtraces) be their actual values, including any object references. This means that storing such a [ScriptBacktrace] will prevent those objects from being deallocated, so it's generally recommended not to do so.
</description>
</method>

By default, this function is only available

I would avoid such wording. These functions are available and work in all builds without any errors or warnings, it's just that the result differs depending on the build and project settings.

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.

2 participants