Saving and Loading with .json

Godot Version

4.4

Question

extends Node

const FILE_NAME = “user://game-data.json”

var player = {
“shortSwordLvl”: 1,
“shortSwordXP”: 0
}

func _process(delta: float) → void:
player[“shortSwordLvl”] = WeaponGlobal.shortSwordLvl
player[“shortSwordXP”] = WeaponGlobal.shortSwordXP

func save():
var file = FileAccess.open(FILE_NAME, FileAccess.WRITE)
file.open(FILE_NAME, FileAccess.WRITE)
file.store_var(player[“shortSwordLvl”])
file.store_var(player[“shortSwordXP”])
file.close()

func load_data():
if FileAccess.file_exists(FILE_NAME):
print(“file found”)
var file = FileAccess.open(FILE_NAME, FileAccess.READ)
player[“shortSwordLvl”] = file.get_var()
player[“shortSwordXP”] = file.get_var()
else:
print(“file not found”)
player[“shortSwordLvl”] = 20
player[“shortSwordXP”] = 20

Very confused with json loading, it outputs file found but the data doesn’t load too

1 Like

Hi, you don’t seem to be using JSON in this code? You’re using the FileAccess methods to get variables and not any JSON methods.

2 Likes

You have to load the file as a string and then hand it to JSON.parse(), which (assuming it’s handed valid JSON) will return a Variant containing nested dictionaries/arrays that have the data from the file.

Seems like your data won’t be loaded for very long.

How are you testing this code? What do you expect to happen versus what really happens?

Make sure to paste with proper formatting

2 Likes

Is there a specific reason you would want to use JSON over Godot’s native serialization as it appears you’re already using?

The file extension doesn’t change how the data is actually stored, as others have pointed out you need to use the JSON methods if you want to store and load JSON. The .JSON extension is just a part of the filename and many applications use this as a clue to it’s contents, but it can easily store any format.

However, if you don’t need to have a person or other applications read or edit the data (which isn’t likely for save data) I would recommend using the built-in serialization. You should then remove the .json extension in the filename to avoid confusion.

The actual problem here though, as @gertkeno pointed out, is that the data is being overwritten immediately after loading. The load_data function needs to also assign the values to the WeaponGlobals variables .

Additional Tips:

  1. When saving and loading the variables, you can actually only store the player variable itself rather than storing, and then retrieve it as a single variable. In my games I always store and retrieve the save data as just a single dictionary this way, much more convenient this way.

  2. Unless you’re accessing the player variable elsewhere (which you don’t appear to be), I would recommend making it local to the save and load_data functions. So in the save function you would declare it and assign the values equal to the WeaponGlobals variables before actually writing it to file. Then in the load_data you declare it and load it from the file, then assign the WeaponGlobals variables to its values. Then you also don’t need the process function.

If you’d like a bit more explanation of these let me know!