The save system stores a version number in each saved game to help you handle different saved game versions. When you save a game, the saved game includes the version number you set in the Save System component's Version field. When you change the type or format of data that's included in a saved game (e.g., if you change the game after release), increment the Save System component's Version value.
To get the version number before loading and restoring a saved game, use SaveSystem.storer.RetrieveSavedGameData(slot) to get the saved game data. Then you can check its version property to determine how to want to handle it. For example, you could use SavedGameData.DeleteData(key) to delete saver data that's no longer valid. Or you could change the data using GetData() and SetData(). Then use SaveSystem.ApplySavedGameData() to apply it. Example:
Code: Select all
var savedGameData = SaveSystem.Storer.RetrieveSavedGameData(1);
if (savedGameData.version == 1)
{
// In version 1, we saved the player's position under "playerPosition".
// But in later versions of the game, we don't save the player's position,
// so remove this save data.
savedGameData.DeleteData("playerPosition");
// Then apply it:
SaveSystem.LoadGame(savedGameData);
}
Code: Select all
public class InventorySaver : Saver // (save system version 1)
{
[Serializable] public class Data
{
public List<int> itemIDs;
}
public override string RecordData()
{
var data = new Data() { itemIDs = new List<int>(PlayerInventory.ItemIDs) };
return SaveSystem.Serialize(data);
}
public override void ApplyData(string s)
{
var data = SaveSystem.Deserialize<Data>(s);
PlayerInventory.ItemIDs = new List<int>(data.itemIDs);
}
}
Code: Select all
public class InventorySaver : Saver // (save system version 2)
{
[Serializable] public class DataV1
{
public List<int> itemIDs;
}
[Serializable] public class DataV2
{
public List<string> itemNames;
}
public override string RecordData()
{
var data = new DataV2() { itemNames = new List<string>(PlayerInventory.ItemNames) };
return SaveSystem.Serialize(data);
}
public override void ApplyData(string s)
{
if (SaveSystem.currentSavedGameData.version == 1)
{
// This is version 1 save data, so we get the List<int> and handle it:
var dataV1 = SaveSystem.Deserialize<DataV1>(s);
PlayerInventory.HandleOldItemIDs(data.itemIDs);
}
else
{
// Version 2, so handle normally for version 2:
var dataV2 = SaveSystem.Deserialize<DataV2>(s);
PlayerInventory.ItemNames = new List<string>(data.itemNames);
}
}
}