Issue with Networking Inventory Objects (Mirror)

Hi everyone,

I am currently working on a multiplayer project using mirror that involves individual players having their own inventories as well as various storage inventories being present in the game world. These inventories are custom classes made up of custom slot classes that ultimately culminate into various integers and integer arrays. I have elected to store individual inventories on clients as I do not see a need to display my players inventories to one another, but I cannot seem to get my Command and ClientRpc methods to work when it comes to updating storage objects.

Currently my method has been to detect when the player interacts with a storage object on their own client which then utilizes the attached StorageObject component on the object which contains a regular method to update the inventory as well as calling a Command DataDeconstructor method which breaks the inventory down into its slots and their associated integers and arrays. This then proceeds to call a ClientRpc DataReconstructor method which passes the data making up each individual inventory slot and updates it.

This is the code called when the client closes the storage inventory

public void UpdateStorage()
    {
        storageObject.GetComponent<StorageObject>().UpdateStorage(playerStorageInventory);
        storageOpen = false;
        storageObject = null;
    }

This is the code on the storage object and the method called by the player - Updates each slot on the inventory attached to the storage object and calls the DataDeconstructor

    public void UpdateStorage(InventoryObject _updatedInventory)
    {
        Debug.Log("called UpdateStorage");
        for (int i = 0; i < storageInventory.GetSlots.Length; i++)
        {
            storageInventory.GetSlots[i].UpdateSlot(_updatedInventory.GetSlots[i].item, _updatedInventory.GetSlots[i].amount, _updatedInventory.GetSlots[i].durability, _updatedInventory.GetSlots[i].storedItems, _updatedInventory.GetSlots[i].storedAmounts, _updatedInventory.GetSlots[i].storedDurabilities, _updatedInventory.GetSlots[i].storedItemsSlotID, _updatedInventory.GetSlots[i].storedItemsSlotAmount);
        }
        DataDeconstructor();
        inUse = false;
    }

This is the DataDeconstructor, is supposed to store the integers/arrays from each slot and pass them to the DataReconstructor

    [Command]
    public void DataDeconstructor()
    {
        Debug.Log("called dataDeconstructor");
        for (int i = 0; i < storageInventory.GetSlots.Length; i++)
        {
            int itemIdstorageInventory = storageInventory.GetSlots[i].item.Id;
            int itemAmount = storageInventory.GetSlots[i].amount;
            int itemDurability = storageInventory.GetSlots[i].durability;
            int[] itemStoredItems = storageInventory.GetSlots[i].storedItems;
            int[] itemStoredAmounts = storageInventory.GetSlots[i].storedAmounts;
            int[] itemStoredDurabilities = storageInventory.GetSlots[i].storedDurabilities;
            int[] itemStoredItemsSlotId = storageInventory.GetSlots[i].storedItemsSlotID;
            int[] itemStoredItemsSlotAmount = storageInventory.GetSlots[i].storedItemsSlotAmount;
            DataReconstructor(i, itemIdstorageInventory, itemAmount, itemDurability, itemStoredItems, itemStoredAmounts, itemStoredDurabilities, itemStoredItemsSlotId, itemStoredItemsSlotAmount);
        }
    }

This is the DataReconstructor, is supposed to update each slot on the storage inventory for each client

    [ClientRpc]
    public void DataReconstructor(int _slotID, int _Id, int _amount, int _durability, int[] _storedItems, int[] _storedAmounts, int[] _storedDurabilities, int[] _storedItemsSlotID, int[] _storedItemsSlotAmount)
    {
        Debug.Log("called clientRpc");
        for (int i = 0; i < storageInventory.GetSlots.Length; i++)
        {
            storageInventory.GetSlots[_slotID].UpdateSlot(storageInventory.GetSlots[_slotID].item, _amount, _durability, _storedItems, _storedAmounts, _storedDurabilities, _storedItemsSlotID, _storedItemsSlotAmount);
        }
    }

My issue is that even though I am not receiving any errors, my inventories will not update across clients and I cannot seem to figure out where I am going wrong. Does anyone have any advice? I may very well be using the Command and ClientRpc calls incorrectly. Any help is appreciated

To reflect changes made by the client you’ll need to pass those changes as parameters of the Command. It looks like you should be passing those storage changes when you call DataDeconstructor.

I realized that my Command wasn’t working due to me attempting to call it on an object outside of the clients authority. I was able to change my code around and successfully update the inventory object across my clients, but I’ve since run into other issues with allowing clients to change the object among over things. Gotta keep trying. Thanks for your help

I’ve run into plenty of issues whilst feeling away along with multiplayer, just shout if you need any help.

Thank you! Maybe you can help me with my current issue. After changing things around, my DataDeconstructor method is now on my player and obtains the netID of the GameObject that represents the StorageObject, which then obtains the StorageObject component on the associated object. It calls the DataReconstructor method once for each slot and updates each individual slot located on the StorageObject components associated inventory. While it seems to work in that my host player can place an item in storage and have it remain there and update on the other players client without issue, my clients can only observe the items placed in storage by the host which are immediately removed when the client interacts and closes the inventory (I currently have it so that the storage inventory updates when a player closes their interface). It also seems that my clients can call the ClientRpc as I am able to send a message to my server console from my client, I just cant actually store items

Any ideas? I can post code if that would help

Hard to say! Did you add the storage object or details of as parameters to DataDeconstructor as I mentioned earlier?

Currently I call the ID and slot count and access my players previously declared storage inventory and then pass it into the DataReconstructor as parameters representing the slot data. The players storage inventory is purely for visually representing each storage object. Whenever a player opens a storage object, their storage inventory populates with the storage objects data and the storage object is updated via the Command and ClientRpc methods when the player closes the interface. I’ll attempt to call the DataDeconstructor using the details as parameters directly and see what happens

You solved my problem, thank you very much for your help. The issue was that I was not passing the data as parameters into the Command method.

1 Like