Help: Updating one dictionary value changes value for same key in all other dictionaries

Godot Version



I am losing my mind over a dictionary saving issue, which I rebuilt here very simplified:

Code example:

#initiate two different empty dictionaries
var save_data: Dictionary = {}
var game_data: Dictionary = {}

func _ready():
#inst. some game data
game_data = {“SomeText”: “Stone”}

#save current game data
save_data = game_data

#change a variable in game data to simulate game running
game_data[“SomeText”] = “Lava”

#print compare values


I absolutely do not understand why the “SomeText” filed in “save_game” also gets changed and would have expected to receive “Lava” “Stone” as outcomes.

I am trying to use this to temporally save a bunch of current game data in “save_game” have to game continuing in “game_data” and be able to jump back to the old data in “save_game”. However, in this example both dictionaries are always 100% the same and do not have any added value

Any hint to what I am missing is highly appreciated!

Best and 1000 thanks

This line doesn’t actually save anything, only makes both variables point to the same dictionary object. Try to use duplicate() instead.


You are my absolute hero. The following fixes the logic and works as a charm:

save_data = game_data.duplicate(true)

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.