0 votes

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.

Godot version v3.34
in Engine by (12 points)

2 Answers

0 votes

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.

by (1,865 points)

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...

0 votes

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.

by (162 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.