Hello, I am using Godot 4.4.1.stable and (attempting to be) using FileAccess as my means of saving and loading data. I do not want to use JSON.
In my SAVEDATA class (which extends Resource), I have an @export Dictionary of 3 elements, called Data. These elements are themselves another class (which also extends Resource) PerLevelSaveData. PerLevelSaveData contains 4 @export variables:
- ScoreNormal (‘normal’ game mode hiscore; int)
- TimeAttackBest (‘time attack’ game mode best time; float)
- ScoreHard (‘hard’ game mode hiscore; int)
- GemstonesCollected (one-time collectibles, all named “Gemstone” with a numerical index as a suffix, added to the dictionary with its collection state (true/false). This is done a maximum of 255 times; Dictionary (string : bool))
I initially attempted to use ResourceSaver.save() and ResourceLoader.load() to save and load SAVEDATA, creating an object that contains a dictionary of 3 instances of PerLevelSaveData. This resulted in ResourceSaver.save() failing, returning error code 15.
Next, I tried using FileAccess to save and load. Saving Data (or another instance of it) yielded a save file appearing. However when attempting to load it, though it recognised it as a dictionary containing 3 elements it would fail to give me any elements of that dictionary in a way that was readable. All elements were EncodedObjectAsID. Attempting to cast them to PerLevelSaveData (the expected type), the element would return as null.
My save function is as follows:
func Save(path : String):
var fa = FileAccess.open(path, FileAccess.WRITE)
fa.big_endian = true
fa.store_var(Data)
fa.close()
My load function is as follows:
func Load(path : String):
var fa = FileAccess.open(path, FileAccess.READ)
fa.big_endian = true
Data = fa.get_var()
fa.close()
This code should directly save and load a dictionary of PerLevelSaveData resources. I am not doing anything out of the ordinary with either the ResourceSaver/ResourceLoader nor the FileAccess method.
Attempting to use instance_from_id(Data[k]) (where k is the level key) still yields the dictionary elements being EncodedObjectAsID.
Lastly, the save file doesn’t actually seem to be saving PerLevelSaveData, as the save file is much smaller than expected.
What exactly am I missing or doing wrong ? How do I properly save my resources such that they can be loaded again ?
Thank you for any help. I’m new to Godot and this is my first project with it after developing in Unity for more than 10 years. Looking forward to making more use of Godot’s tools.