JSON Serialization
For saving more advanced data structures, you can serialize them to JSON and store them in the Save system.
Last updated
// Note: Only @ao_serialize fields are written
Save.set_json(player, "progress", ref progress);load_progress :: proc(player: Player) -> Player_Progress {
progress := new(Player_Progress);
if !Save.try_get_json(player, "progress", ref progress) {
// New player (or malformed JSON) already has the class defaults.
}
return progress;
}load_progress_and_migrate :: proc(player: Player) -> Player_Progress {
progress := new(Player_Progress);
if !Save.try_get_json(player, "progress", ref progress) {
// The class defaults already describe a new player.
}
// Migrate forward
if progress.version < 2 {
progress.version = 2;
// Example: v2 introduced unlocked_skins (initialize it)
progress.unlocked_skins = .{};
}
// Write back the migrated version
Save.set_json(player, "progress", ref progress);
return progress;
}JSON :: struct {
serialize :: proc(obj: ref $T) -> string;
try_deserialize :: proc(json: string, out_value: ref $T) -> bool;
}