Serialization

So you want to 'Save' an Inventory, huh?

Serializing an Inventory is simple. The InventoryState class will help you keep track of what an Inventory looks like. In order to keep things efficient we only serialize the reference indexes for lookup and not the actual item data. This keeps saves very small and compact.

Doing this means that we are not 1:1 with data being saved and data in the game. It requires some translation work in order to get the data back and forth properly but fortunately it's all auto-magic for you!

What is stored

InventoryState result = MyCharacterInventory.ToState();

This will create an InventoryState instance for you. We only need to keep track of a few things in order to restore an inventory to it's state. Here's what is actually stored:

  1. The Vault Index ID of the Configuration: int

  2. The Vault Index IDs of the Items in each slot: List<int>

  3. The stack sizes of the items in each slot: List<int>

It's important to note that we have the means to restore the Configuration and Content of an inventory we still have to actually perform the translation from indexes to data.

How to restore

To-and-From-JSON is built into the InventoryState class, but in order to get an Inventory back into a particular state you can use the Initialize overload to force the Inventory to initialize itself and pull from Vault all of the content it needs based on the raw data that was saved.

public virtual void Initialize(IUseInventory owner, InventoryState state)

This automatically handles converting everything in the InventoryState so you don't need to worry about it. You should be able to serialize/deserialize the InventoryState however you wish and as long as you throw a valid state at this method it will work.

Last updated