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)
    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!

in Engine by (521 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/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?

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/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()
by (1,650 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.open(data_json, 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.