Search code examples
c#unity-game-engineinventory

I have created an inventory System but i dont know how to proceed


I have everything in place, but i have a problem setting the items that are created in the inventory to the actual slots. i made this so that there is a better overview of the items.

this is the code for the inventory:

// Amount of Slots in inventory defined here!!!
[SerializeField]
private Slot[] slots = new Slot[10];
public List<Item> invItems = new List<Item>();




public void CheckSlot(Item item)
{
for (int i = 0; i < slots.Length; i++)
{
    if (slots[i].isEmpty == false)
    {
        //call a method.
        if (item.isStackable)
         {
             slots[i].amount = item.amount;
         }
     }

     if (slots[i].isEmpty == true)
     {
            //call another method.
            //run some code.
            
            slots[i] = new Slot(item);
         return;

     }
 }
 }

public void UpdateAmount(Item _item,Slot _slot)
{
    _slot.amount = _item.amount;
}

public void CreateItem(Item item)
{
    Item invItem1 = new Item();
    invItem1 = item.Copy();
    invItems.Add(invItem1);
}


public void InventoryAdd(Item item)
{
    //check if the item is already in the inventory
    //if true check if it is stackable and the new amount is less than maxStackable amount
    //if false add the item to the invItems list and set the slot to false            


    

    

     if (invItems.Count != 0)
     {

     for (int i = 0; i < invItems.Count; i++)
        {
           
            if (invItems[i].ItemID == item.ItemID)
            {
                
                if (invItems[i].amount + item.amount <= invItems[i].maxStackable)
                {
                    
                    invItems[i].amount += item.amount;

                    CheckSlot(invItems[i]); //

                    return;
                }
                if (invItems[i].amount < invItems[i].maxStackable)
                {
                    if (invItems[i].amount + item.amount > invItems[i].maxStackable)
                    {

                        
                        int nextAmount = invItems[i].maxStackable - invItems[i].amount;
                        invItems[i].amount += nextAmount;


                        Item invItem1 = new Item();
                        invItem1 = item.Copy();
                        invItem1.amount = invItem1.amount - nextAmount;
                        invItems.Add(invItem1);

                        

                        return;
                    }
                }
            }
        }
     }

    Item invItem = new Item();
    invItem = item.Copy();
    invItems.Add(invItem);

and the Item script :

public string name;
public  int ItemID;
public GameObject prefab;
public Sprite icon;
public int amount = 1;

public bool isStackable;
public int maxStackable = 5;

public Item()
{

}

public Item(string _name, int id,GameObject _prefab, int _amount, bool stackable,int maxStack)
{
    name = _name;
    ItemID = id;
    prefab = _prefab;
    amount = _amount;
    isStackable = stackable;
    maxStackable = maxStack;
}

public Item Copy()
{
    Item copy = new Item();
    copy.name = this.name;
    copy.ItemID = this.ItemID;
    copy.prefab = this.prefab;
    copy.amount = this.amount;
    copy.isStackable = this.isStackable;
    copy.maxStackable = this.maxStackable;

    return copy;
}

and the code for a slot:

public bool isEmpty = true;

public Slot()
{

}

public Slot(Item _item)
{
    this.name = _item.name;
    this.ItemID = _item.ItemID;
    this.isStackable = _item.isStackable;
    this.prefab = _item.prefab;
    this.icon = _item.icon;
}

the thing is i do not know how i can set the Items i have created in the inventory to the slot. maybe i need to cleanup my code, and check for each of those things in one go instead of seperate?


Solution

  • What I understand from your description is that you have multiple slots and each slot can hold one or more item. So,Instead of adding them through constructor like below:

    public Slot(Item _item)
    {
        this.name = _item.name;
        this.ItemID = _item.ItemID;
        this.isStackable = _item.isStackable;
        this.prefab = _item.prefab;
        this.icon = _item.icon;
    }
    

    Maintain a list of items in slot class and have a special method to add items to slot. Something like this:

            public List<Item> ItemsInSlot {get;set;}
            public void AddItems(IEnumerable<Item> items)
            {
                ItemsInSlot.AddRange(items);
            }
    

    I am assuming you already have some mechanic in place to trigger adding items to a slot. Like a button in UI or dragging item(s) to slot.