Well this sucks.
My first game is nearly finished and suddenly my save system has died. Don't know why - once it was working, I didn't edit it.
Got this error:
E 0:00:04.926 get_as_text: File must be opened before use.
E 0:00:04.927 call: Error parsing JSON at line 0:
<C++ Source> modules/gdscript/gdscript_functions.cpp:1215 @ call()
<Stack Trace> Globals.gd:122 @ _load_function()
StageSelect.gd:10 @ _ready()
I already have error handling built in with a default value function should there be no save. It's been working this whole time until just now.
Global Functions:
func _save_data_update(): ## compiles data
_saveData = {
"_thoughtsCleared" : _thoughtsCleared,
"_secondsStillness" : _secondsStillness,
"_lilypadsKissed" : _lilypadsKissed,
"_blossomsWatched" : _blossomsWatched,
"_momentsOfSilence" : _momentsOfSilence,
"_centerBalance" : _centerBalance,
"_breathsTaken" : _breathsTaken,
"_wisdomRead" : _wisdomRead,
"_splashesCreated" : _splashesCreated,
"_zenAchieved" : _zenAchieved,
"_mandalaRotations" : _mandalaRotations,
"_globalMusic" : _globalMusic,
"_globalSine" : _globalSine
}
return _saveData
func _save_function(): ## saves to file
var _myFile = File.new()
_myFile.open(_GAMESAVE, File.WRITE)
_save_data_update()
_myFile.store_line(to_json(_saveData))
_myFile.close()
func _load_function(): ## loads if present (logic in main scene)
var _saveFile = File.new()
_saveFile.open(_GAMESAVE,File.READ)
var _text = _saveFile.get_as_text()
_saveData = parse_json(_text)
_thoughtsCleared = _saveData._thoughtsCleared
_secondsStillness = _saveData._secondsStillness
_lilypadsKissed = _saveData._lilypadsKissed
_blossomsWatched = _saveData._blossomsWatched
_momentsOfSilence = _saveData._momentsOfSilence
_centerBalance = _saveData._centerBalance
_breathsTaken = _saveData._breathsTaken
_wisdomRead = _saveData._wisdomRead
_splashesCreated = _saveData._splashesCreated
_zenAchieved = _saveData._zenAchieved
_mandalaRotations = _saveData._mandalaRotations
_globalMusic = _saveData._globalMusic
_globalSine = _saveData._globalSine
func _default_stats(): ## default levels if nothing present
_thoughtsCleared = 0
_secondsStillness = 0
_lilypadsKissed = 0
_blossomsWatched = 0
_momentsOfSilence = 0
_centerBalance = 0
_breathsTaken = 0
_wisdomRead = 0
_splashesCreated = 0
_zenAchieved = 0
_mandalaRotations = 0.0
_globalMusic = true
_globalSine = false
And then here is the function called at ready at the main scene:
func _saveCheck(): ## if no save file, load default values
var _playerSave = File.new()
if _playerSave.file_exists("user://gamesave.json"):
Globals._load_function()
print("User data loaded!")
else:
Globals._default_stats()
print("Default data loaded.")
Weird thing - it's crashing at the end of an animation signal, not on load. There's no memory function there.