The Godot Q&A is currently undergoing maintenance!

Your ability to ask and answer questions is temporarily disabled. You can browse existing threads in read-only mode.

We are working on bringing this community platform back to its full functionality, stay tuned for updates. | Twitter

0 votes

Hey there!
I'm using a JSON to save the game data. The code that I'm using (on a singleton) is this one, so I just call update_data from somewhere else:
(The variable data is a dic that has the json content)

#Called from other scripts to update data to be stored
func update_data(key, subKey, value, sender=null, log_change=true):
    if not data.has(key) or not data[key].keys().has(subKey):
        print("ERROR [%s]: Invalid key/value, unable to update data" % sender)
        data[key][subKey] = value
        if log_change:
            print("SAVE [%s]: Game saved with success!" % sender)

func _ready():

#Loads the JSON 
func load_json():
    var file =, file.READ)
    data = parse_json(file.get_as_text())

#Write the new data to the json
func save_game(data):
    var file =, file.WRITE)

In the engine, everything works fine but if I export the game with debug, I get no errors but the json data is lost when I close the game.
If I export without debug the game does not open (insta crash)


Thanks in advance!

in Engine by (519 points)

At any rate, you probably want to be saving user game data in a user directory outside of the game/application directory. You can use user://path/to/ This way you can provide updates to the game by entirely replacing the game files, and the save data will be unaffected.

If you manually save some data in the JSON file and then start the game, does it load it properly then?

I think that I did something wrong then. There is not a .json whithin the exported files, only a .pck and the .exe.

This is how my export resources are configured and my json is at res://data/game-data.json. While playing in the editor, it works just fine, I can manually save some data as you said.
enter image description here

How do I save a file in the user:// ? I did not know about this possibility.

1 Answer

0 votes

Try changing your data_json variable to be user://game-data.json in scripts/ You don't need to actually worry about where it is saved - Godot and the OS will worry about that. See this article for more details.

It's possible that the json file was not exported, and so you were trying to read from a file that does not exist. In that case, you should definitely also add a check to see if the json file exists, and if not, create an empty json file.

func load_json():
    var file =

    if not file.file_exists(data_json):
         # just save an empty Dictionary to initialize
         save_game({}), file.READ)
    data = parse_json(file.get_as_text())
by (1,663 points)

I did some tests and it says that the file exists but when I print file.get_path() it prints a blank line. When running on the editor it prints res://data/game-data.JSON

Are you calling file.get_path() before or after calling, file.READ)?

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 Frequently asked questions and 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 [email protected] with your username.