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

Get help using Construct 2

Post » Sun Jul 31, 2016 10:31 pm

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.
Mistakes were made.
B
55
S
28
G
115
Posts: 1,709
Reputation: 65,116

Post » Mon Aug 01, 2016 5:01 am

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
Tired of crappy file hosts that are crappy? Get DROPBOX - https://db.tt/uwjysXJF
Moderator
B
84
S
34
G
40
Posts: 3,032
Reputation: 30,499

Post » Mon Aug 01, 2016 6:48 am

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...
Mistakes were made.
B
55
S
28
G
115
Posts: 1,709
Reputation: 65,116

Post » Mon Aug 01, 2016 11:28 am

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
Tired of crappy file hosts that are crappy? Get DROPBOX - https://db.tt/uwjysXJF
Moderator
B
84
S
34
G
40
Posts: 3,032
Reputation: 30,499

Post » Mon Aug 01, 2016 3:28 pm

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.
plugin-mkarray-arrays-on-runtime_t179478

Just a suggestion.
B
33
S
18
G
28
Posts: 2,493
Reputation: 20,950

Post » Mon Aug 01, 2016 4:09 pm

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)
B
94
S
33
G
123
Posts: 5,450
Reputation: 78,463

Post » Mon Aug 01, 2016 6:15 pm

R0J0hound wrote: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.
Mistakes were made.
B
55
S
28
G
115
Posts: 1,709
Reputation: 65,116

Post » Tue Aug 02, 2016 8:50 am

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
Tired of crappy file hosts that are crappy? Get DROPBOX - https://db.tt/uwjysXJF
Moderator
B
84
S
34
G
40
Posts: 3,032
Reputation: 30,499


Return to How do I....?

Who is online

Users browsing this forum: No registered users and 33 guests