# Pick X value of array based on "contains value"?

Get help using Construct 2

### » Mon Mar 24, 2014 8:24 am

Is there a way to set an array's CurX value by some sort of "contains value?". I'm trying to check my inventory array to see if the item is already in the inventory, and if so, add to the quantity. If it's not in the inventory, add it to first "unassigned" array slot.
B
52
S
13
G
1
Posts: 578
Reputation: 4,635

### » Mon Mar 24, 2014 12:52 pm

I'm not sure what you mean by 'contains value', is it any value or a set of specific ones?
In case you know what the values are you can do something like this for the check.
if array.curX = value1 OR array.curX = value2..
or even have all the value in a 2nd array and run a a for, and check if array1.curX = array2.curX
By unassigned array slot do you mean the 1st one witha 0 value?
B
11
S
4
G
1
Posts: 272
Reputation: 1,643

### » Mon Mar 24, 2014 3:35 pm

You probably want something like this. Pay attention to the itemIndex function. It returns -1 if the item isn't contained in the inventory, otherwise, it returns the index of the item.
B
81
S
33
G
27
Posts: 1,029
Reputation: 21,143

### » Mon Mar 24, 2014 5:49 pm

@Magistross - I think this is almost what I'm looking for, but I'm having trouble understanding why this is working and how I can apply it to mine

It doesn't look like the "itemindex" function is being called at all; is it only being used to store a variable?
B
52
S
13
G
1
Posts: 578
Reputation: 4,635

### » Mon Mar 24, 2014 6:05 pm

It is called by the addItem function. It first check if an item exists (index != -1), if so, add to its quantity, if not, add a new entry to the inventory array, and set its quantity to 1.
B
81
S
33
G
27
Posts: 1,029
Reputation: 21,143

### » Mon Mar 24, 2014 6:49 pm

@Magistross

Here's what I'm trying to do slightly based on some of the ideas in yours:

I know its not, but in my mind, this is what this current event list is doing:

Checks every X value to determine if ItemName exists. If it does, set the loopindex (which would be the CurX of the array) to a variable to add to the existing item's quantity. If it doesn't exist but finds an "unassigned" value, adds the item to inventory. If neither are true (meaning the item is not currently in the inventory and there is no bag space), do nothing.

I'm beginning to think loopindex isn't working like I think it is, however.
B
52
S
13
G
1
Posts: 578
Reputation: 4,635

### » Mon Mar 24, 2014 6:58 pm

You should be using CurX, not loopindex. loopindex is for the System:For event.
B
72
S
22
G
288
Posts: 3,862
Reputation: 158,460

### » Mon Mar 24, 2014 7:01 pm

I don't think loopindex works with the For each axis of the array object, use array.curX or array.curY instead. I don't think stop loop works either... so maybe you should just use a plain "for loop".
B
81
S
33
G
27
Posts: 1,029
Reputation: 21,143

### » Mon Mar 24, 2014 7:08 pm

I missed the Stop loop action. If you want to use Stop loop just switch to the System:For event with loopindex. Magistoss' example show this (System:For).
B
72
S
22
G
288
Posts: 3,862
Reputation: 158,460

### » Mon Mar 24, 2014 10:00 pm

@blackhornet & @Magistross - beautiful! Using a "for" event loop with loopindex works perfectly. Thanks so much!
B
52
S
13
G
1
Posts: 578
Reputation: 4,635