- Home /
I need help fixing my inventory sorting script.
Basically, this is part of a script that lets me stack items of the same name within each item type list. The problem is, when I first add an item to the curInventory, it causes the size of the stack added to double because it is counted twice.
function stackItems(){
var lastItemIndex = curInventory.Count - 1; //the index of the last item in the list
for(var i: int = 0; i < curInventory.Count;i++){ //for all items in the list
if(curInventory[lastItemIndex].name == curInventory[i].name){ //if the name of the last item in the list is the same as the name of the
//current item being checked
if (curInventory[lastItemIndex].stackable == true){ //if the item is able to be stacked
if(curInventory[i].stackCount < curInventory[i].maxStack){ //if the number of items currently in the stack is
//less than the max number of items able to stack
if(
curInventory[i].stackCount += curInventory[lastItemIndex].stackCount; //increase the stack count of the current item by
//the number of items in the new stack. This is what causes new items to be counted twice.
if (lastItemIndex !=0){
curInventory.RemoveAt(lastItemIndex);
}
}
}
}
}
}
I could really use a fresh set of eyes to help me figure out how I could adjust my current script so that it doesn't count items added to a blank curInventory twice.
Answer by aceronn · Apr 09, 2014 at 04:08 AM
I know this is prob not gonna get read, you have it going 1, then when stacked its taking the 1 and adding 1, which makes 2, but when it stacks again its not registering as 2 + 1, its going to register as 2 + 2. You need to do something like
stackcount++
then itll register as 1 + 1 = 2, 2+1 = 3 and so on. Sorry for messed up format, first answer on this xD
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Where can i begin to learn how to make an inventory. (Javascript). 1 Answer
Inventory Help. 2 Answers
I'm trying to add items to an inventory. 1 Answer
Inventory script Help Please 2 Answers