hey,

I assume it's simple problem but I'm stuck. In my project I have multiple dictionaries for storing data. The idea was to edit/change those dictionaries during game, then merge them, save in one save file, and on next game start - read saved file, and pass specific keys to multiple dictionaries. And it works fine. But at one point keys are getting duplicated, and dunno where I can fix that. Woudl be grateful for help :)

  1. with this I merge passed dictionaries and save in one file:
    func write_data(url:String, MainDictPlusInv:Dictionary, char1:Dictionary):
    
    MainDictPlusInv["character1"] = char1
    	
    file.open(url, File.WRITE)
    file.store_line(to_json(MainDictPlusInv))
    file.close()
  1. This is func for calling saving:
    func save_data() -> void:
    DataTables.write_data(url_PlayerData, {"inventory": inventory}, {"character1": character1})
  1. and on game startup, I start with loading my saved file (passing whole file in one temp dict - playerData) and then passing it to specific dicts:
    inventory = playerData["inventory"]
    character1 = playerData["character1"]

The problem is that after loading file me keys the duplicated. And instead of:

"character1":{"hp":6776,"kills":100,"xp":20}

i have:

"character1":{"character1":{"hp":6776,"kills":100,"xp":20}}

after multiple saves - i still have only 1 doubled key. I guess it's something in point 2, where I pass dict with added key but... dunno how to rewrite it to store this in a correct way.

It looks like you're getting the duplicate because you're passing in a dictionary with the key "character1":

DataTables.write_data(url_PlayerData, {"inventory": inventory}, {"character1": character1})

But then you're adding that to MainDictPlusInv under another key "character1":

MainDictPlusInv["character1"] = char1

Which is doing this: MainDictPlusInv["character1"] = {"character1": character1}

Change that write_data call to:

DataTables.write_data(url_PlayerData, {"inventory": inventory}, character1)

and I think you'll get what you're looking for.

11 days later

Thank you :) Figured it just before you posted, correctly assumed it's something simple. But thank you nonetheless :)

2 years later