JSON 序列化
要保存更高级的数据结构,你可以将其序列化为 JSON 并存储到保存系统中。
最后更新于
// 注意:只会写入 @ao_serialize 字段
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) {
// 新玩家(或格式错误的 JSON)已经拥有类默认值。
}
return progress;
}load_progress_and_migrate :: proc(player: Player) -> Player_Progress {
progress := new(Player_Progress);
if !Save.try_get_json(player, "progress", ref progress) {
// 类默认值已经描述了一个新玩家。
}
// 向前迁移
if progress.version < 2 {
progress.version = 2;
// 示例:v2 引入了 unlocked_skins(进行初始化)
progress.unlocked_skins = .{};
}
// 将迁移后的版本写回
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;
}