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

0 favourites
  • 11 posts
From the Asset Store
It's here!
$4.95 USD
Creepy and nasty horror track. It's terrifying from the beginning till the end. Goosebumps guaranteed
  • 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.

  • 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.

  • 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.

  • Have a look at the array's index expression.

  • 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).

    As you can see i have "9mm-bullets" value inside array, but IndexOf is returning -1

  • 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).

  • > 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

  • 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.

  • 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?

  • 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

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • 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

    Thanks! I did that and now it works!

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)