Hello,
I'm trying to implement a savegame feature. As part of that I need to save the player's weapons into the JSON save file. The weapons are stored as an array of classes ("Weapon" is a custom class). The array is variable. When the player collects a weapon, this weapon is added to the array.
So, at the time of coding, I do not know the size and content of the array to be saved.
I add all the relevant data into a dictionary. I also tried to do that with the weapons array:
var save_dict = {
"weapons" = player_weapons
}
Looking into the JSON, this produces a line that looks like this:
"weapons":["[Reference:7495]","[Reference:9769]","[Reference:9853]","[Reference:10503]","[Reference:10652]","[Reference:10755]"]
However, loading this back in by doing
player_weapons = json_data.get("weapons")
Results in two things:
1) Whatever gets loaded back in apparently doesn't work. For example, the class "Weapon" has a property "name". However, when I try to access that property, I get "Invalid get index 'name'". So apparently it doesn't load the stuff back in properly.
2) When I try to print the array, I see that the reference numbers are different (e.g. instead of the first reference having the number 7495, it suddenly has 4075).
Apparently I cannot save arrays like this. So how would I do that?
I found posts stating how to manually enter an array into the JSON, like
"arr" : [1, 2, 3]
however, since I don't know the length, contents or order of contents in the array at the time of coding, how would I solve this?