Can't load dictionary correctly.

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

I can’t load a dictionary after saving it, and after searching the internet can’t seem to fix it. Looking inside the save file, it looks fine to me. Any way to make it work, am I missing a step here?

func _on_Save_pressed():    
var save_file = File.new()
save_file.open("user://savefile.save", File.WRITE)
save_file.store_line(str(AutoLoad.Cash))
save_file.store_line(str(AutoLoad.Level))
save_file.store_line(str(AutoLoad.XP))
save_file.store_line(str(AutoLoad.MaxXP))
save_file.store_line(str(AutoLoad.BeatStore))
save_file.store_line(str(AutoLoad.BossesKilled))
save_file.store_line(str(AutoLoad.CurrentWeapon))
save_file.store_line(str(AutoLoad.CurrentArmor))
save_file.store_line(str(AutoLoad.CurrentHelmet))
save_file.store_line(to_json(AutoLoad.weapons))
save_file.store_line(to_json(AutoLoad.armors))
save_file.store_line(to_json(AutoLoad.helmets))
print("Saved!")
save_file.close()

func _on_Load_pressed():
var save_file = File.new()
if not save_file.file_exists("user://savefile.save"):
	return
save_file.open("user://savefile.save", File.READ)
AutoLoad.Cash = int(save_file.get_line())
AutoLoad.Level = int(save_file.get_line())
AutoLoad.XP = int(save_file.get_line())
AutoLoad.MaxXP = int(save_file.get_line())
AutoLoad.BeatStore = bool(save_file.get_line())
AutoLoad.BossesKilled = int(save_file.get_line())
AutoLoad.CurrentWeapon = str(save_file.get_line())
AutoLoad.CurrentArmor = str(save_file.get_line())
AutoLoad.CurrentHelmet = str(save_file.get_line())
AutoLoad.weapons = parse_json(save_file.get_line())
AutoLoad.armors = parse_json(save_file.get_line())
AutoLoad.helmets = parse_json(save_file.get_line())
print("Loaded")
save_file.close()

Thank you for your time.

:bust_in_silhouette: Reply From: deaton64

Hi,

Does this help: save a global Dictionary to json and load it back

And I quote:

i found a solution for this problem. so am sharing this for anyone in need

just add when saving var2str

file.store_line(to_json(var2str(inventory)))

and when loading str2var

inventory = str2var(data)

this fixed my issue it was parsing the object as a text so godot can’t read it that is why it wouldn’t show in inventory slots after loading but by using var2str and str2var it helped saving and loading the object as it is.

Still doesn’t work. It just becomes ‘Nil’. The dictionary I use also contains both strings, variables, and booleans. I don’t know what i’m doing wrong, and i’d rather not replace the dictionary with a bunch of messy variables…

Boopy | 2021-10-13 22:02

:bust_in_silhouette: Reply From: r.bailey

One thing that looks off to me on your logic is you are storing some variables as a Json format and other’s you are not. The built in Engine Json parser is not smart enough to deduce where some values in Json starts and ends and will fail on the parse(Read) if you mix like this(pretty sure, but not 100 percent it is like this.)

If you want to use all text values per line like this you will have to manually handle every line and string compare against what you are expecting. There is no magic method or function that will deduce this for you.

The other option is to use the Json classes built into the engine, but this expects the whole file to be in proper Json format. I will link an answer to using a Json parser that I put out a bit ago for another user, to show you an example of what I mean.

Json example loader

Hope this leads you to a solution.