How do I get min/max for a variable amount of values?

0 favourites
  • 8 posts
From the Asset Store
15 High Quality SoundTracks, and 10 Fanfares for your game! :D
  • Is there a way to use the min/max() expressions in a situation where the number of values to evaluate is not predetermined?

    My current workaround is to push the set of values into an array, sort, then get the front or back value. I was wondering if there were any different methods.

  • I don't think there is, since you don't know what the values will be until such time as you require them.

    How are you using this in your project? Can you store the last known value spread in a couple of variables (instance or global depending what they're for?) and compare those (until they're changed)?

    I'm trying to think of a situation where you would need to compare unknown values - and my tiny brain isn't coping, lol.

    ~Sol

  • Sorry it's not the value that's unknown, it is the number of them.

    For example, I can get the distance to each image point on a sprite, and I want to track the nearest/farthest one. But the number of image points may vary, depending on the animation frame/state, so I can't just use the min/max expressions...

  • Ah right I get you...

    Hmm... I would probably use a dictionary with keys for the things I wanted to track (ImagePointCount or DistanceToControlPoint, etc) for each object - sticking them in a container together. This way you can just reference the keys to get whatever the changing/weird value is. I find it a lot easier than messing around with arrays. I use them exclusively for every character/enemy in any game/idea/prototype as an easy way to record every obscure detail about any given instance - not to mention the ease of saving and loading as JSON, and even nesting dictionaries into a single dictionary for that specific purpose.

    I guess it's pretty much the same as you're already doing - but instead of counting cell positions with arbitrary methods, the dictionary just makes it a little more "friendly". My brain doesn't brain when I have to start using too many obscure numbers and token counts, etc... but that's just me.

    *EDIT*

    Failed to mention, regarding the original question - you can then simply do a key count on the dictionary as well if necessary for the actual amount of values.

    ~Sol

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • With this plugin, you have expressions representing the max/min value, without having to sort.

    Also, filling the array is awwsome simple.

    On top you can make/delete an array, and therefor have not to worry about the dimensions.

    Just a suggestion.

  • You can also do it with a loop. Here's an example to get the low and high x:

    global number low=0

    global number high=0

    every tick

    --- set low to infinity

    --- set high to -infinity

    for each sprite

    --- set low to min(sprite.x, low)

    --- set high to max(sprite.x, high)

  • You can also do it with a loop. Here's an example to get the low and high x:

    global number low=0

    global number high=0

    every tick

    --- set low to infinity

    --- set high to -infinity

    for each sprite

    --- set low to min(sprite.x, low)

    --- set high to max(sprite.x, high)

    That is exactly what I was looking for, thanks R0J0. I was having trouble wrapping my head around how to keep only the lowest or highest value in a loop. I had tried comparing each value to the one before it, but that would result in issues where the order the values were checked would matter.

  • Oooohhhh riiiight.... NOW I understand what you meant.

    R0J0 always has the easy answer though... I would have done it differently (because I'm not awesome) but my solution would have been far more complicated.

    ~Sol

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