x


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.

more ▼

asked Jul 23, 2013 at 02:44 AM

Malwallace12 gravatar image

Malwallace12
1 3 3 5

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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

more ▼

answered Apr 09 at 04:08 AM

aceronn gravatar image

aceronn
0

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x5986
x3892
x441
x3

asked: Jul 23, 2013 at 02:44 AM

Seen: 354 times

Last Updated: Apr 09 at 04:08 AM