Game Development Reference
In-Depth Information
newSaveState.Inventory = new List<string>();
foreach (var item in input.Inventory)
{
newSaveState.Inventory.Add(item.name);
}
return newSaveState;
}
This is fairly simple; we are just copying the properties across. Of course, you only need
to copy savable properties. If there are values the player cannot affect, then there is no
need to save them. Of note is that for the player's inventory, where we only capture the as-
set name of each item. This is because we don't need to serialize InventoryItems
themselves (the game already knows about them), only the ones the player has.
Tip
If you have items that can wear out, then you will also need to create a savable state for
InventoryItem so you can save just the important bits or changeable values.
Instead of creating a Save model, you can simply tag each property you want to serialize
with a [SerializeField] attribute (including private variables) and those that you
don't want to serialize with a [NonSerialized] attribute.
However, in practice, this can cause trouble or confusion when debugging your saved
data. In my personal experience, it's better to define a separate Save model so that you al-
ways know what you are dealing with.
Then, you simply need another extension method to do the reverse, as follows:
public static Player LoadPlayerSaveState(this
PlayerSaveState input, Player player)
{
player.Age = input.Age;
player.Armor = input.Armor;
player.Damage = input.Damage;
player.Defense = input.Defense;
player.Faction = input.Faction;
player.Health = input.Health;
player.Level = input.Level;
Search WWH ::




Custom Search