How do I make my item only open if there is room in my Inventory?

I am trying to only opening an item if there is room in my inventory. Here is a segment of my code.

if(e.isMouse && e.type == EventType.mouseDown && e.button == 1)
						{
							if(item.itemType == Item.ItemType.Consumable)
							{
								UseConsumable(slots*,i);*
  •  					}*
    
  •  				}*
    

//////////////////////////////////////Segment//////////////////////////////////
void UseConsumable(Item item, int slot)

  • {*

  •  switch(item.itemID)*
    
  •  {*
    
  •  	case 22:*
    
  •  	{*
    
  •  		inventory[slot] = new Item();*
    
  •  		AddItem(20);*
    
  •  		AddItem(23);*
    
  •  		AddItem(24);*
    
  •  		AddItem(25);*
    
  •  		break;	*
    
  •  	}*
    
  •  }*
    
  • }*

  • void RemoveItem(int id)*

  • {*

  •  for(int i = 0; i < inventory.Count; i++)*
    
  •  {*
    

_ if(inventory*.itemID == id)_
_
{_
_ inventory = new Item();
break;
}
}
}*_

* void AddItem(int id)*
* {*

* for(int i = 0; i< inventory.Count; i++)*
* {*
_ if(inventory*.itemName == null)
{
for(int j = 0; j < database.items.Count; j++)
{
if(database.items[j].itemID == id)
{
inventory = database.items[j];
}
}
break;*_

* }*

* }*
* }*

* bool InventoryContains(int id)*
* {*
* for (int i = 0; i < inventory.Count; i++)*
_ if (inventory*.itemID == id) return true;
return false;
}*

I included the parts after use consumable so you can have a better idea. I tried doing an if statement but I can’t even think what I would put for it to get this to work. The overall goal is to only open the my item if there is four slots of inventory available. Any Ideas?_

You could add a new function to find the count of empty slots:

 int GetInventorySpace()
 {
     int count = 0;
     for (int i = 0; i < inventory.Count; i++)
         if (inventory*.itemID == -1) count += 1;*

return count;
}
Then just check if GetInventorySpace() >= 4. The other option would be to have a variable change when you add or remove items from your inventory.

You may try this:

if(item.itemType == Item.ItemType.Consumable && i < slot.Length - 4)
{
    UseConsumable(slots*,i);*

}
Here i < slot.Length - 4 will prevent to go in this condition there are less than 4 slot available.

Got it to work! Did this instead inside the GetInventorySpace()

if(inventory*.itemID == -1)*