Trying to have multiple Inventories by item type

Hi!
I would like to get some help. I have been trying to implement multiple inventory’s in my game. I would like to have a different inventory for different item types (Currently 3 types).

I have followed the inventory course offered on Udemy and still have the same mechanics as in the course but with some minor tweaks.

Currently I seem to have it partially working. I have created 3 different inventory’s. When the player picks up an item it goes in the correct inventory. But when I drag & drop items from any where to their inventory the item always spawns in the first inventory I have created.

Here is a pic of what I am trying to do

Like mentioned I have 3 inventories (Mask, M-components, Materials). Currently, when I pick up items they spawn in the correct inventory. But When I drag & drop from Gear to the inventory’s or change an item slot to another slot within the same inventory, the Item is always spawned in the first inventory(Mask) no matter what.

These are the inventory scripts on the player:

Since all inventory’s have the same name I made a InventoryPlayerList script just to have a reference of my inventory’s for easier access.

Item category’s are made with an enum in the InventoryItem Script:

public enum ItemCategory
    {
        Mask,
        MComponent,
        Material
    }
    public abstract class InventoryItem : ScriptableObject, ISerializationCallbackReceiver
    {
        protected ItemCategory itemCategory = ItemCategory.Mask;
        public ItemCategory Category => itemCategory;
        public ItemCategory itemCategoryType;

My guess is in the dragItem script, in the GetContainer Method, I would have to find some way to change the IDragDestination that is in the container. But I may be wrong on that one.

Let know if there are any script or other you would like me to share. I will be glad to share!

Thanks in advance!
Cheers,

There are two significant structural problems with this approach, and I’m partly responsible for this as it follows an approach I wrote quite a while back. I’ve since seen the flaws, so it’s high time to rectify this. Simply put, the multiple Inventory components on the same character are not going to work for two reasons:

  • The Saving System will only save the last Inventory it finds on the character, and will restore that inventory into all copies in RestoreState. This is a structural issue with the savingSystem itself, but it’s easier to fix the Inventory than it is to fix the structural deficit in the Saving System (I may approach that at a later date).
  • Linking multiple Inventories in the Inspector actually only links the first Inventory, the other two inventories will never get seen by the script. This is a bug in Unity, and the only real fix would be to put each Inventory on a different child object and then link them, but if you do that, the Saving System still won’t pick them up.

A better solution is to adjust Inventory to store a different Array for each Item Category, like bags. This can be accomplished by using a

Dictionary<ItemCategory, InventorySlot[]> slots;

Or, this may be a good time to convert the InventoryItem array into a Dictionary as well

Dictionary<ItemCategory, Dictionary<int, InventorySlot>> slotDict;

The advantage of this is less storage space needed when slots are empty. If the slot doesn’t exist, then it’s empty and doesn’t take space. Because of the way Dictionaries work using Hash Tables, they can be nearly as performant as Arrays, but without the pre-cached memory.

This will require a rewrite of just about every major function in Inventory, and will take some time.
The advantage to this approach is that it’s more flexible when completed. Adding a new Inventory category is as easy as adding to the enum. The disadvantage is it’s initial complexity.

An alternate method of handling this is to subclass Inventory, to have a different Inventory Type for each type of inventory item. This would require less refactoring on the Inventory side (making some methods virtual in Inventory and overriding them in the subclasses. The advantage to this is it’s relatively simple to implement, the disadvantage is that it isn’t as flexible, as it requires creating a new Inventory subclass for each ItemCategory.

I’m going to work on a version of each over the next week, but it will take some time.

Saving : I had not looked into the saving flaw yet (did not try to save all 3 inventory’s yet…) but I was wondering if it could work on all inventory’s or not.

Linking multiple Inventories in the Inspector: You are right, I had tried putting the inventory’s a 3 separate child object to the player. It did not work out. Which is why I ended up making a script on the player with inventory fields that reference the inventory’s on the player. Using it as the middle man for other scripts. But probably does not work for the save system either.

Even tho your first solution would be a bigger pain to implement because of complexity, I am wondering if it would still be the best option in the long run because of flexibility. But I am still interested / curious in making the inventory as a subclass. I had thought of doing this but was not sure how to approach it yet.

No worries if it takes a while to get a functional solution! Good things take time!

I still have lots to learn but I will try to work on a solution as well.

Privacy & Terms