I just can’t seem to work out how to add Items to my Inventory when it already exists and the amount i’m adding go’s over the ‘maxStack’ amount?
Code:
List<Item> FilterItems(Item newitem){
List<Item> result = new List<Item>();
foreach(Item item in PlayerInventory.Inventory){//For 'Item' in Player's Inventory...
if(item.ID == newitem.ID){//If newItem's ID = the loops current items ID...
if((item.currentStack + newitem.currentStack) < item.maxStack){//If the current stack + newItems stack is less than the maxStack...
print ("Add");
result.Add(item);
} else{
print ("Do something here!");
}
}
}
return result;
}
public void AddItem(Item newItem){
if(newItem.maxStack > 0){//If there are 1 or more stacks
List<Item> stacks = FilterItems(newItem);
if(stacks.Count > 0){//if there are more than 1 returned items
stacks[0].currentStack += newItem.currentStack;
} else {//when there is no stack yet or all stacks are full
PlayerInventory.Inventory.Add(newItem);
}
} else{//Item is not stackable
PlayerInventory.Inventory.Add(newItem);
}
}
Please ask if you need anything else?