- Home /
Question by
stridernara · Oct 15, 2014 at 03:14 PM ·
inventoryinventory system
Inventory stack system problam
So i've made an Inventory with a stack system but when I add stacks if stacks are added to every items in the inventory instead of the one I want to add to they all get the same amount of stacks
public void AddItem(int id, float stacks)
{
for (int i = 0; i < inventory.Count; i++)
{
if(inventory[i].itemName == null)
{
for(int j = 0; j < database.items.Count; j++)
{
if(database.items[j].itemID ==id)
{
if(!InventoryContains(id))
{
inventory[i] = database.items[j];
}
else
{
AddStacks(id,stacks);
}
}
}
break;
}
}
}
public void AddStacks (int id,float stacks)
{
for (int i = 0; i < inventory.Count; i++)
{
for (int j = 0; j < database.items.Count; j++)
{
if(database.items[j].itemID == id)
{
inventory[i].itemStacks = stacks + inventory[i].itemStacks;
}
}
}
}
public void RemoveStacks (int id, float stacks)
{
for (int i = 0; i< inventory.Count; i++)
{
if(inventory[i].itemID == id)
{
if(inventory[i].itemStacks > stacks||inventory[i].itemStacks > 1)
{
inventory[i].itemStacks = inventory[i].itemStacks - stacks;
}
else
{
RemoveItem(id);
}
}
}
}
public void RemoveItem(int id)
{
for(int i = 0; i < inventory.Count; i++)
{
if (inventory[i].itemID == id)
{
inventory[i] = new Item();
break;
}
}
}
public bool InventoryContains (int id)
{
bool result = false;
for (int i = 0; i < inventory.Count; i++)
{
result = inventory[i].itemID == id;
if (result)
{
break;
}
}
return result;
}
public bool InventoryContainsStacks (int id, float stacks)
{
bool result1 = false;
bool result2 = false;
for (int i = 0; i < inventory.Count; i++)
{
result1 = inventory[i].itemID == id;
result2 = inventory[i].itemStacks == stacks;
if (result1&&result2)
{
break;
}
}
return result1;
return result2;
}
Comment