![]() |
Attention | Topic was automatically imported from the old Question2Answer platform. |
![]() |
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.
How do I save a file in the user://
? I did not know about this possibility.
fpicoral | 2019-02-18 21:35