JSON not saving after exporting the game

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

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)
    else:
        data[key][subKey] = value
        save_game(data)
        if log_change:
            print("SAVE [%s]: Game saved with success!" % sender)

func _ready():
    load_json()

#Loads the JSON 
func load_json():
    var file = File.new()
    file.open(data_json, file.READ)
    data = parse_json(file.get_as_text())
    file.close()
                
#Write the new data to the json
func save_game(data):
    var file = File.new()
    file.open(data_json, file.WRITE)
    file.store_line(to_json(data))
    file.close()

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)

Project: https://github.com/feRpicoral/HeistMasters

Thanks in advance!

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/file.save. 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?

Eric Ellingson | 2019-02-18 20:01

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.

fpicoral | 2019-02-18 21:35

:bust_in_silhouette: Reply From: Eric Ellingson

Try changing your data_json variable to be user://game-data.json in scripts/GameData.gd. 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 = File.new()

    if not file.file_exists(data_json):
         # just save an empty Dictionary to initialize
         save_game({})

    file.open(data_json, file.READ)
    data = parse_json(file.get_as_text())
    file.close()

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

fpicoral | 2019-02-20 02:27

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

Eric Ellingson | 2019-02-20 16:48