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!