Is there a way to set the debugger to "stop" when a variable changes its value?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By gonzalezharry

So, I have a variable which is set by code to a value, then, somehow the value is being changed. Actually hard to find where since it isn’t being done explicitly in the code. So, Is there a way to tell the debugger “when a line changes this variable’s value, stop at the line you’re currently at”?
That would help me a lot!

:bust_in_silhouette: Reply From: jgodfrey

That’s a more advanced debugging feature that’s (unfortunately) not available in Godot’s debugger - AFAIK.

:bust_in_silhouette: Reply From: LoafWard

I got this working using a setter function on a var that calls print_stack, then pauses the game.

extends Node

var my_var = "Initial Value" setget debug_stackdump_pause

func debug_stackdump_pause(new_value):
if new_value != "Some Initial Value I Expected":
	print_debug("my_var changed! WHY!? New value: ", new_value)
	print_stack()
	get_tree().paused = true

Anytime the var gets changed from an external source the getter function is executed. It won’t ever run if somehow this node is changing itself.

If the var in question is built-in (like node3D.transform) you could probably write something similar using _set() instead.

Seems great! I’ll try it out since the variable that’s being changed is a control’s margins.

gonzalezharry | 2023-02-08 15:33

the variable that’s being changed is a control’s margins.

In that case, I’d guess the likely culprit here is a parent container control.

jgodfrey | 2023-02-08 16:11