Picking highest values out of an array

0 favourites
  • 6 posts
From the Asset Store
Supports 1D, 2D, 3D arrays. Import and export arrays in JSON format
  • Hello there!

    Can anyone help me with the following problem:

    I have an array1 that looks like this:

    y0= 'Name1' 'Name2' Name3' 'Name4' 'Name5' 'Name6'

    y1= 18 21 10 23 15 13

    Now I would like to push the top 3 of the names with the highest values into another array2

    How do I set up this loop comparing the three highest values?

    Thanks in advance for anyone who has tips for me!

    Jess

  • Repeat 3 times

    Sub events -

    Local variable "highestValue"

    Local variable "index"

    Array for each xy

    Array.cury=1

    Array. Curvalue>highestValue

    -> Set index to array.Curx

    -> Set highestValue to array.Curvalue

    Next same-level event, not sub-

    ->Push back Array2 array.At(index,0)

    ->Set Array2 at Array2.width-1,1 to array.at(index,1)

    ->Delete array at index

    If you need to preserve the original array, you can save array.asjson to a variable beforehand and load it again after.

  • Thanks for your quick and extensive reply!

    I see some nice tips in the conditions, but it isn't working yet for the moment.

    Now I get every name in the new array in the same order: Name1, Name2, Name 3,...

    Some questions to understand your advice better:

    • Is there a condition missing before the second same-level part or are those other actions that are following on the other conditions?
    • Why is it necessary to delete something from the first array?
    • This one I don't understand: Set Array2 at Array2.width-1,1 to array.at(index,1)

    I will keep trying to fix the problem meanwhile, so no hurry answering this.

    I'm already very thankful to help me think!

    • Is there a condition missing before the second same-level part or are those other actions that are following on the other conditions?

    There is no condition for these actions, they just need to run after the Array for each xy loop finishes, but only should run once so it can't be a sub event of the for each XY event.

    • Why is it necessary to delete something from the first array?

    The for each xy event finds and records the highest value in the array. If you don't delete it, it will find the same value again in the next repeat of the event.

    • This one I don't understand: Set Array2 at Array2.width-1,1 to array.at(index,1)

    Because we're using push back, this sets the y=1 value at the back of the array.

    X=array.width-1 - this means the last value of the array on the x axis

    Y=1 - where you keep the value (Y=0 was the name)

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Also remember to set a trigger for the top level repeat event! Otherwise it will run every tick and just keep putting values from the old array into the new array until there are none left. Also Array 2 should start out with width 0 height 2.

    Here is what it should look like:

  • Wow, it's working now!! (I've had a stupid mistake, didn't change code to the properties of my array)

    This is fantastic, man!

    Thank you very, very much for your patience & big help!!

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