How do I find location of array item by it's value

Just started using Construct 2? Post your questions here

Post » Fri Jan 29, 2016 5:25 pm

Right now i am making inventory system for my game and i have some issues with it.
I would like to get location (X,Y) of the item, based on it's value.

My inventory array looks like this:
Frame Number
Amount
Name

With frame number i am selecting picture to be shown in inventory, if amount is higher than 0. But with "Name" parameter i would like to find specific item in inventory and to increase/decrease it's amount.

Let's say that i want to reload my gun. In game i have "Handgun" and "9mm ammo clip" items in my inventory. When player reload it's gun, one 9 mm ammo clip is removed from inventory. My problem here is how to refference this specific item in array. Right now i am adding items from inventory with function that has two parameters, number of frame to be shown and amount. When i am removing i must specify X location in array and amount to be removed.
B
14
S
3
G
1
Posts: 95
Reputation: 1,015

Post » Fri Jan 29, 2016 5:57 pm

ButcherZV wrote:Right now i am making inventory system for my game and i have some issues with it.
I would like to get location (X,Y) of the item, based on it's value.

My inventory array looks like this:
Frame Number
Amount
Name

With frame number i am selecting picture to be shown in inventory, if amount is higher than 0. But with "Name" parameter i would like to find specific item in inventory and to increase/decrease it's amount.

Let's say that i want to reload my gun. In game i have "Handgun" and "9mm ammo clip" items in my inventory. When player reload it's gun, one 9 mm ammo clip is removed from inventory. My problem here is how to refference this specific item in array. Right now i am adding items from inventory with function that has two parameters, number of frame to be shown and amount. When i am removing i must specify X location in array and amount to be removed.


Can't really post my .capx but here is a screen shot on how I handle ammo collection and checking array. Perhaps it will be of some assistance.
Image
Image Image Image
B
63
S
19
G
6
Posts: 325
Reputation: 7,994

Post » Fri Jan 29, 2016 6:13 pm

facecjf if i understand this event sheet, you have already specified on what array slot different weapons will be and then you are just adding to this value.
In my game inventory is little different - let's say that player will first pick up health box, then after few minutes handgun and bullets, so health box will be on slot 1, handgun on slot 2 and bullets on slot 3. But some other player can pickup handgun first, then bullets, then health box and now order will be different. I can't expect order of pickup, so i can't put always healthbox on first place, handgun on second and so on.
B
14
S
3
G
1
Posts: 95
Reputation: 1,015

Post » Fri Jan 29, 2016 6:24 pm

Have a look at the array's index expression.
B
55
S
29
G
19
Posts: 1,520
Reputation: 25,670

Post » Fri Jan 29, 2016 6:33 pm

ramones wrote:Have a look at the array's index expression.


And how do i use this IndexOf expression? I saw it in the manual, but i don't understand what parameters it requires.

EDIT: Ok i used IndexOf inside my RemoveItems function parameter, but it's returning -1 value. RemoveItems function requires two parameters, first is slot in inventory (X coordinate) and second parameter is amount of items to be removed. So function looks like this RemoveItems(arrInventory.IndexOf("9mm-bullets"), 1).

Image

As you can see i have "9mm-bullets" value inside array, but IndexOf is returning -1
B
14
S
3
G
1
Posts: 95
Reputation: 1,015

Post » Fri Jan 29, 2016 8:17 pm

ButcherZV wrote:facecjf if i understand this event sheet, you have already specified on what array slot different weapons will be and then you are just adding to this value.
In my game inventory is little different - let's say that player will first pick up health box, then after few minutes handgun and bullets, so health box will be on slot 1, handgun on slot 2 and bullets on slot 3. But some other player can pickup handgun first, then bullets, then health box and now order will be different. I can't expect order of pickup, so i can't put always healthbox on first place, handgun on second and so on.


Correct. I Added a true / false value in my array. Basically it sets a value of 1 = collected or 0 = not collected. Order of items I wasn't as concerned with. With a simple event and the true / false value from the array you can display only items collected by checking the array for items with a value of 1 (collected).
Image Image Image
B
63
S
19
G
6
Posts: 325
Reputation: 7,994

Post » Fri Jan 29, 2016 8:44 pm

facecjf wrote:
ButcherZV wrote:facecjf if i understand this event sheet, you have already specified on what array slot different weapons will be and then you are just adding to this value.
In my game inventory is little different - let's say that player will first pick up health box, then after few minutes handgun and bullets, so health box will be on slot 1, handgun on slot 2 and bullets on slot 3. But some other player can pickup handgun first, then bullets, then health box and now order will be different. I can't expect order of pickup, so i can't put always healthbox on first place, handgun on second and so on.


Correct. I Added a true / false value in my array. Basically it sets a value of 1 = collected or 0 = not collected. Order of items I wasn't as concerned with. With a simple event and the true / false value from the array you can display only items collected by checking the array for items with a value of 1 (collected).


Yes, you have simpler, but effective inventory. My inventory array start empty and it's updated when player pick up item, so i can't use your method, but thanks anyway for replying :D
B
14
S
3
G
1
Posts: 95
Reputation: 1,015

Post » Fri Jan 29, 2016 8:51 pm

You would have to put the weapon name in the X axis to find it with IndexOf. Otherwise you'd have to loop through the array and compare each value.
B
55
S
29
G
19
Posts: 1,520
Reputation: 25,670

Post » Fri Jan 29, 2016 10:32 pm

ramones wrote:You would have to put the weapon name in the X axis to find it with IndexOf. Otherwise you'd have to loop through the array and compare each value.


I don't understand it, isn't it already on X axis? You think that if i switch place of "item name" and "frame number" parameter it will work? Or what i need to do in order to this work?
B
14
S
3
G
1
Posts: 95
Reputation: 1,015

Post » Fri Jan 29, 2016 11:50 pm

I mean if you swap frame number and name in the array so name is the first value at (x, 0).
In the debugger it should look like:
9mm-bullets,7,1
handgun,1,2
B
55
S
29
G
19
Posts: 1,520
Reputation: 25,670

Next

Return to Beginner's Questions

Who is online

Users browsing this forum: No registered users and 1 guest