0 votes

I'm working on my first save system and it consists of multiple dictionaries. Simply the players character, the enemy, and a dictionary for the icon of each item they have equipped, so anywhere between 2 and 10 depending. However I can't figure out how to parse past the first dictionary. They're all saved on the same JSON file, which looks like this with three dictionaries:

{"AttackPower":10,"Boss":"res://Bosses/SkeletalArcher.tscn","Character":"res://Characters/PitFighter.tscn","DodgeChance":10,"Energy":100,"HP":110,"Potions":4,"SleepChance":5,"Speed":2,"_AttackPower":10,"_BossHP":80,"_DodgeChance":10,"_Energy":100,"_HP":89,"_Speed":2,"fv":151.5}
{"UpgradeIcon":"res://Upgrades/SandmansBlackjackIcon.tscn"}
{"AttackPower":7,"DodgeChance":20,"HP":100,"Speed":1.4,"_DodgeChance":20,"_HP":80}

and the code for parsing it looks like this:

save_game.open("res://saverun.save", File.READ)
while not save_game.eof_reached():
    var current_line = parse_json(save_game.get_as_text())

It works great for everything in the first dictionary, but the UpgradeIcon key and all the keys from the boss are completely ignored. I'm sure I'm just making a beginners mistake but I couldn't seem to find a solution in the documentation.

asked May 17 in Engine by MustacheLincoln (12 points)
edited May 17 by volzhs

1 Answer

0 votes

You could just make them into one json:
{ "character" : {"AttackPower":10,"Boss":"res://Bosses/SkeletalArcher.tscn","Character":"res://Characters/PitFighter.tscn","DodgeChance":10,"Energy":100,"HP":110,"Potions":4,"SleepChance":5,"Speed":2,"_AttackPower":10,"_BossHP":80,"_DodgeChance":10,"_Energy":100,"_HP":89,"_Speed":2,"fv":151.5}, "enemy" : {"UpgradeIcon":"res://Upgrades/SandmansBlackjackIcon.tscn"}, "icon" : {"AttackPower":7,"DodgeChance":20,"HP":100,"Speed":1.4,"_DodgeChance":20,"_HP":80} }

or

[ {"AttackPower":10,"Boss":"res://Bosses/SkeletalArcher.tscn","Character":"res://Characters/PitFighter.tscn","DodgeChance":10,"Energy":100,"HP":110,"Potions":4,"SleepChance":5,"Speed":2,"_AttackPower":10,"_BossHP":80,"_DodgeChance":10,"_Energy":100,"_HP":89,"_Speed":2,"fv":151.5}, {"UpgradeIcon":"res://Upgrades/SandmansBlackjackIcon.tscn"}, {"AttackPower":7,"DodgeChance":20,"HP":100,"Speed":1.4,"_DodgeChance":20,"_HP":80}]

answered May 17 by HalfTough (93 points)

How would you write the save function to make them save like that?

You need to declare a single array which will contain all your dictionaries, then save that array into a JSON file.

What would be the most elegant way to write that?

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.