- Home /
Adding Item to an Inventory.
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 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(playerInventory.inventory.count > 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);
}
}
So basically, say i have One stack of 7 wood and i want to add 5. How would I code it to fill up the current stack and put the remaining 2 wood into a new stack?
Please ask if you need anything else?
Answer by KellyThomas · Dec 29, 2013 at 05:14 PM
This would be one approach.
Points to consider:
itemAmount
, how much of the newItem we are trying to allocateavailableSpace
, how much room is available in any given item slotmaking sure we look after any remaining
itemAmount
once we have finished our search
List FilterItems(Item newitem){ int itemAmount = newitem.currentStack; List result = new List(); foreach(Item item in PlayerInventory.Inventory){ if(item.ID == newitem.ID) { if(item.currentStack < item.maxStack){ int availableSpace = item.maxStack - item.currentStack; if (itemAmount < availableSpace) { item.currentStack += itemAmount; itemAmount = 0; } else { item.currentStack = item.maxStack; itemAmount -= availableSpace; } print ("Add"); result.Add(item); } } } if (itemAmount > 0) { newitem.currentStack = itemAmount; result.add(newitem); return result; }
When i first add something, say the currentstack is 2 it will add 2 fine, but then if i add 3 the next time it will add 4?
Step though it:
first it takes the itemAmount
then for each item with matching ID in the inventory
if it has room:
calculate the availableSpace
if itemAmount is less than availableSpace:
add all of itemAmount to currentStack
set itemAmount to zero
else:
fill currentStack to capacity
decrease itemAmount by the same value
add this item to the results list
if any itemAmount remains and it to the results list
I have tried to replicate the logic of your code and the request in the question. Some of the logic seems strange to me but I don't know the purpose of FilterItems()
and how it fits into your game.
At the moment results
will only contain items to which it has changed the currentStack
value, but never removes any elements from the player inventory. Depending on your purpose that me mean that items are missing or duplicated.
It is more intended to show one way of solving the problem of handling "overflow" rather than to provide code you can paste into your project.
Got it achieving what i want now. Thanks, using what you have said above i have gotten rid of the 'Filter' function and have managed to squeeze in modified code. :) Thanks very much!
Hey, here is a tutorial, that show you how to create an inventory, that has the most common operations: https://www.youtube.com/watch?v=$$anonymous$$LaGkc87dDQ