Search Unity

Issue with unit(-s) inventory saving

Discussion in 'Scripting' started by Rukas90, Mar 26, 2019.

  1. Rukas90

    Rukas90

    Joined:
    Sep 20, 2015
    Posts:
    169
    Hello there,
    Currently I'm working on this game and in that game I have a bunch of units that I can create and each of these units can carry some items inside their personal inventory.

    Item is just a ScriptableObject with some data inside and the inventory is just a private list of items inside the Unit class. The problem is that how do I save each unit inventory? I can save at any point in the game and I use Binary to serialize all game data into a file.

    I do not use ScriptableObject for the inventory to hold the list of Items, because I am able to create as many units as I want and that would mean that all units would use the same inventory. And creating a new ScriptableObject Instance for each unit doesn't sound too ideal to me. That's why I just have a private list inside the unit class instead.

    But the issue is that I heard it's not recommended to serialize a list of ScriptableObjects (if possible at all, I'm not sure). Is that true? If so, are there any kind of workarounds to this? Thank you
     
  2. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    One way to do this would be to assign some sort of ID number to every item in your game, then serialize the ID numbers of the items that should be in your inventory. When deserializing, search your library of items for one with a matching ID number.

    (Note that if you take this approach, you should avoid renumbering any of your items when you release a new version; otherwise, old save files will load with different items in them. That means you probably don't want to assume that your ID numbers will be sequential.)