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

Get help using Construct 2

Post » 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
40
S
12
G
1
Posts: 532
Reputation: 4,147

Post » 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?
Please tag me with @caiorosisca if your post is refering to me :). I don't always follow the topics I answer.
B
11
S
4
G
1
Posts: 272
Reputation: 1,643

Post » 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
71
S
30
G
25
Posts: 984
Reputation: 19,503

Post » 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 :P

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

Post » 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
71
S
30
G
25
Posts: 984
Reputation: 19,503

Post » 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:

Image

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
40
S
12
G
1
Posts: 532
Reputation: 4,147

Post » Mon Mar 24, 2014 6:58 pm

You should be using CurX, not loopindex. loopindex is for the System:For event.
ImageImageImage
B
71
S
22
G
240
Posts: 3,734
Reputation: 133,994

Post » 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
71
S
30
G
25
Posts: 984
Reputation: 19,503

Post » 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).
ImageImageImage
B
71
S
22
G
240
Posts: 3,734
Reputation: 133,994

Post » Mon Mar 24, 2014 10:00 pm

@blackhornet & @Magistross - beautiful! Using a "for" event loop with loopindex works perfectly. Thanks so much!
B
40
S
12
G
1
Posts: 532
Reputation: 4,147


Return to How do I....?

Who is online

Users browsing this forum: dop2000, gonzdevour and 31 guests