How to find what is changing a variable?

Godot Version

4.2.1.stable

Question

I’ve got a weird bug where some arbitrary variable is synchronized across instances, most likely caused by weird spaghetti code, and I cannot for the life of me figure out what is causing them to sync. Is there a debug feature I can use to track when and what is changing a variable?

1 Like

You can turn that variable into a property, which has a getter and setter, and inside the setter, call the print function to potentially help figure out where the variable is being set. Here is some code that you can adapt:

var myVar = DEFAULT_VAL: # Replace your original var with this. `= DEFAULT_VAL` is optional.
    get:
        return myVar 
    set(value):
        myVar = value
        print(SOMETHING_GOES_HERE) # For example, `name`, or `owner`, or ...

Will give it a shot, thanks : D

1 Like

From my testing, the setter only gets called on creation, yet the variable still changes.

@export var stat_block: Dictionary = {"damage": 10.0, "reload_time": 1.75, "reserve_ammo": 60, "max_ammo": 10, "inaccuraccy": 1.0, "proj_speed": 40.0, "auto": false, "spec1": 0.0, "spec2": 0.0, "spec3": 0.0}:
	get:
		return stat_block
	set(value):
		print("STAT BLOCK CHANGED TO " + str(value))
		stat_block = value
1 Like

The problem is that setters only get called whenever you quite literally do an equal sign assignment = for the entire dictionary. Since you are probably just mutating the individual dictionary values (and not overwriting the entire dictionary), you actually probably want to put the print statement inside the get block (the print argument should be "STAT BLOCK THAT WAS GOTTEN WAS " + str(stat_block)), but even then, I am honestly not 100% sure if that would work. Still though, you should try. The theory is that the getter should run anytime the dictionary is accessed, potentially including when it’s values get mutated.

1 Like

From what I tested, the whole stat block is being set, as other values in it also are synced. Another odd thing is that when I change the stat block in the inspector, (in the remote tree when the game is running) the syncing problem goes away until the weapon is swapped, where the stat block resets. (Most likely an error on my part)

Ok, looking over the whole thing, It’s a complete mess. As painful as it will be this whole system is in need of probably it’s third full rewrite. Working with such a weird inventory combined with multiplayer is just impossible. Time to burn myself out and not get anything done for the next year.