Serializing inventory data that uses scriptable objects.

How does one go about saving an inventory? My initial thoughts would just be to save some kind of itemID, but now that requires I make some kind of table to look them up when I am loading the data. (Maybe just another item library object that has a key/value pair of all items) That wouldn’t be that complicated, but it seems to leave room for error. Forgetting to link items to the library or maybe breaking links by changing the id or deleting items. Is there any elegant way of doing this? Thanks!

First, don’t save ScriptableObjects. Think of ScriptableObjects as read-only parts of your project, where you define the qualities of an inventory item when you are making the game.

The actual inventory data would be “here is a list of the items I have and how many of each I have, as well as perhaps any changes that have happened to them (such as wear and tear).”

That’s the only part you would ever serialize and load/save.

For linking that inventory to the ScriptableObjects that define them, I highly recommend just using strings, and to help preclude typing errors, make a public enum for the strings and use them directly with .ToString().

public enum WeaponNames
{
    Sword,
    LongSword,
    Dagger,
    // ONLY ADD NEW ITEMS HERE:
}

Then when you want to access it, just take the .ToString() of that as a handy way to ensure you don’t mistype it.

Unfortunately when Unity serializes enums, it only serializes the number, so once you start using enums in assets (scenes, prefabs, or scriptable objects) you must take care to only add to the end of the enum list, never change their order, never delete any of them.