Mutiple inventory's saving and loading is working but need help drag&drop

Hi,

I have almost got multiple inventories working! I am able to save all 3 of my inventories and load them at game start up. Loaded items go in the correct inventory and in the correct slot. Picked up items also go in the correct inventory.

I ended up creating a new save system for my 3 inventory’s( Mask, M-Component and Materials)

The 3 inventory scripts are on the player with an enum(ItemCategory) to differentiate between the 3 inventory instances.

public class Inventory : MonoBehaviour
    {
        public ItemCategory itemCategory;`

The Inventory’s itemCategory is set in the inspector for each inventory.

I created a class named PlayerInventoryList to manage the instances of the inventory scripts on the player when :

  • saving and loading.
    -InventoryUI script ( checks which inventory instance to use)
    -On the Pickup script when picking up items.(Checks which inventory instance to put the item into)
public class InventoryPlayerList : MonoBehaviour
{
    public Inventory maskInventory;
    public Inventory mComponentInventory;
    public Inventory materialInventory;

}

The issue I am having is when I drag & drop items. Dragging work fine. I am able to click on a item and it will follow the mouse. But when I drop a item in inventory it will only work in my first inventory(Mask).

So:

  • If i drag&drop from Mask inventory to Mask inventory it works fine.

But:

  • if I drag&drop from M-component inventory to M-component inventory the item will snap back to it’s original M-component inventory slot and not the destination slot.

  • If I drag&drop an item from M-component inventory to Mask inventory the item will be removed from M-component inventory and appear in the correct slot in Mask inventory.


My guess is that my problem is in the DragItem script with the destination container. Because it seems to know where the item comes from and initiates the drag, but drop only works on my first inventory. I am unsure how to check the destination container actually is the problem.

Forgot to mention that drag&drop items from any inventory to equipment slot(Gear) is working correctly.

Any ideas/pointers/ help would be real helpful.
Thanks in advance!
Cheers!

The first thing that comes to mind is that the InventorySlotUI within the MComponents and Materials tabs are not getting the correct Inventory. If they are picking up their Inventory component from Inventory.GetPlayerInventory(), then it will pick up the first Inventory on the Player, which may not be the correct one (i.e. Mask).

Looks like you have crafted a good solution for dealing with the Saving System (which would gather the data from each Inventory and then over write the first two Inventory saves with the last one, and then it would restore the Inventory to all three with the contents of the last one). Well done sorting that.

Thanks, took me a while to figure it out. I had initially tried to do the solutions you had mentioned in the previous thread but i had a hard time getting it right so I tried it this way out of curiosity. i remembered I had purchased Easy save not to long ago on special and decided to give it a try. It made the job a lot easier.

here is the save and load scripts I made:

public class SaveInventory : MonoBehaviour
{
    public InventoryPlayerList inventoryPlayerList;

    public Inventory maskInventory;
    public Inventory mComponentInventory;
    public Inventory materialInventory;

    private void Awake()
    {
        maskInventory = inventoryPlayerList.maskInventory;
        mComponentInventory = inventoryPlayerList.MComponentInventory;
        materialInventory = inventoryPlayerList.materialInventory;
    }

    public void Save_Inventory()
    {
        //--------Save inventory size-------
        ES3.Save("MaskInventorySize", maskInventory.inventorySize);
        ES3.Save("MComponentteInventorySize", mComponentInventory.inventorySize);
        ES3.Save("MaterialInventorySize", materialInventory.inventorySize);

        //--------Save Slots----------------
        SaveInventorySlots();
    }

    public void SaveInventorySlots()
    {
        var maskSlotStrings = maskInventory.PrepareInventorySlotsToSave();
        ES3.Save("MaskInventorySlotRecord", maskSlotStrings);

        var mComponnentSlotStrings = mComponentInventory.PrepareInventorySlotsToSave();
        ES3.Save("MComponentInventorySlotRecord", mComponnentSlotStrings);

        var MaterialSlotStrings = materialInventory.PrepareInventorySlotsToSave();
        ES3.Save("MaterialInventorySlotRecord", MaterialSlotStrings);
    }
}
public class LoadInventory : MonoBehaviour
{
    public InventoryPlayerList inventoryPlayerList;

    public Inventory maskInventory;
    public Inventory mComponentInventory;
    public Inventory materialInventory;

    private void Awake()
    {
        maskInventory = inventoryPlayerList.maskInventory;
        mComponentInventory = inventoryPlayerList.MComponentInventory;
        materialInventory = inventoryPlayerList.materialInventory;
    }

    public void Load_Inventory()
    {
        //-------Load inventory size-------
        maskInventory.inventorySize = ES3.Load<int>("MaskInventorySize");
        mComponentInventory.inventorySize = ES3.Load<int>("MComponentteInventorySize");
        materialInventory.inventorySize = ES3.Load<int>("MaterialInventorySize");

        //-------Load inventory slots record------
        LoadInventorySlots();

        //-------Updateinventory----------
        maskInventory.InventoryUpdated();
        mComponentInventory.InventoryUpdated();
        materialInventory.InventoryUpdated();
    }

    public void LoadInventorySlots()
    {
        maskInventory.PrepareInventoryToLoad();
        mComponentInventory.PrepareInventorySlotsToSave();
        materialInventory.PrepareInventorySlotsToSave();
    }
}

Here is what i did in the inventory script for saving and loading:


 public object PrepareInventorySlotsToSave()
        {
            var slotStrings = new InventorySlotRecord[inventorySize];
            for (int i = 0; i < inventorySize; i++)
            {
                if (slots[i].item != null)
                {
                    slotStrings[i].itemID = slots[i].item.GetItemID();
                    slotStrings[i].number = slots[i].number;
                }
            }
            return slotStrings;
        }


        public void PrepareInventoryToLoad()
        {
            // Load after after startup: only mask inventory loads. Maybe I have to save and load the item categories on the items as well for it to load properly  
            //var slotStrings = new InventorySlotRecord[inventorySize];


            if (itemCategory == ItemCategory.Mask)
            {
                inventorySize = ES3.Load<int>("MaskInventorySize");
                slots = new InventorySlot[inventorySize];
                Debug.Log("mask inventory size loaded");


                //----Load slot test----
                var slotStrings = new InventorySlotRecord[inventorySize];
                slotStrings = ES3.Load("MaskInventorySlotRecord", slotStrings);
                for (int i = 0; i < inventorySize; i++)
                {
                    slots[i].item = InventoryItem.GetFromID(slotStrings[i].itemID);
                    slots[i].number = slotStrings[i].number;
                }
                //if (inventoryUpdated != null)
                //{
                //    inventoryUpdated();
                //}
                Debug.Log("Mask slots loaded");

            }

            if (itemCategory == ItemCategory.MComponent)
            {
                inventorySize = ES3.Load<int>("MComponentteInventorySize");
                slots = new InventorySlot[inventorySize];
                Debug.Log("Mcomponent inventory size loaded");


                //----Load slot test----
                var slotStrings = new InventorySlotRecord[inventorySize];
                slotStrings = ES3.Load("MComponentInventorySlotRecord", slotStrings);
                for (int i = 0; i < inventorySize; i++)
                {
                    slots[i].item = InventoryItem.GetFromID(slotStrings[i].itemID);
                    slots[i].number = slotStrings[i].number;
                }
                //if (inventoryUpdated != null)
                //{
                //    inventoryUpdated();
                //}

                Debug.Log("Mcomponent slots loaded");
            }

            if (itemCategory == ItemCategory.Material)
            {
                inventorySize = ES3.Load<int>("MaterialInventorySize");
                slots = new InventorySlot[inventorySize];
                Debug.Log("Marerial inventory size loaded");
            }

            if (inventoryUpdated != null)
            {
                inventoryUpdated();
            }
        }

The only issue I have with the save/Load is If I load the inventory’s after the game has started with a clickable UI button it will only load the Mask inventory. I may have to look into it a some point but I am happy that it loads correctly on start up for now. I put a bool in the inventory scripts to choose in the inspector if i want to load on startup or not. In the future I may come back to try to optimize it if I can,

Here’s a quick look at how I have i have it set up. Each bubble is a script.

As for the InventorySlotUI within the MComponents and Materials tabs are not getting the correct Inventory thanks for pointing me there! I ended up finding the bug! In the Inventory UI script I put debug logs here:

public int MaxAcceptable(InventoryItem item)
        {
            Debug.Log("InventorySlotUI.MaxAcceptable is asking if inventory" + inventory.itemCategory.ToString() + "has space for: " + item.ToString());
            if (inventory.HasSpaceFor(item))
            {
                Debug.Log("There is space for item: " + item.ToString() + " in : " + inventory.itemCategory.ToString() + " Inventory");
                return int.MaxValue;
            }
            return 0;
        }

Then here in the inventory script:

public bool HasSpaceFor(InventoryItem item)
        {
            if (item.itemCategoryType != itemCategory)
            {
                Debug.Log("Inventory.HasSpaceFor itemCategory returned false");
                Debug.Log("Category of the item cheking space for: " + item.itemCategoryType.ToString() + "Current Inventory Category: " + itemCategory.ToString());

                return false;
            }
            return FindSlot(item) >= 0;
        }

The debug.logs in the inventory made me click that item in the Mcomponent inventory did not have the same item categorie as the inventory. So I whent to look in the InventoryItem script I think and realized I had 2 itemCategorie variables by accident an my scripts where referring to the wrong one! So I removed one and linked scripts back to the good one and boom. It worked!

Now drag&drop is working good in all inventory’s. Thanks again!

Excellent! Well done!

I haven’t played with EasySave, but, as the course Saving System and my own Json Saving System are now so engrained in my brain… I just can’t imagine using another system anymore. Before picking up the Saving System here, I used something very similar to an Android Bundle type system.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms