Closest value to some value check possible?

If we have variable we can check if its equal to 10 lower than 10 or greater than 10

BUT is there a way to check which variable is closest to some number?
Like i can now check if object is closest to some X or Y pos and pick it
There is literally condition for that

Can i somehow do it with variable?
Imagine i have 3 variables with values 2 5 7
Could i somehow pick one that is closest to 10?

Or i would need to use some extension for it to be possible

I may be wrong, but can’t you check min,max, in math list?
oh w8 i guess you may want any number in range too…
mmm i dunno…maybe with some subtraction and addiction formula to check which is closest to chosen number…it should be like to check a distance

some formula here may help?

1 Like

That would solve the problem if we input numbers manually
What if i have 100 enemies or 200 and i want to pick one with lowest HP variable?

Or maybe highest?

And now i think there should be in gdevelop easy way to check that
Yet i can’t find any

Wel…i use the pick action/conditions…to pick "1"of the closest enemy to player (but it could be enemy Hps, status, anythying i guess)…and it works…but they’re objects, …not vars.

So, I got this inelegant approach, ugly even.

Use an object each to represent every variable. And also one object to represent target.
Place objects and position on either x or y axis.
Position depending on the value of the variable.
Use pick nearest object.

Like, if first variable value is 2, first object is positioned at x2,y0
if second variable value is 5, second object is positioned at x5,y0
Then position the object for the target number the same way.

If we’re looking at the HP of enemies, each enemy is going to get linked to an HP object that’s positioned according to their respective HP values.
Closest HP object to the target object wins.

Yeah, it’s definitely inelegant. Essentially doubles the number of objects on the scene. And it’s not exactly the easiest, unlike with functions that work with just one line of event.
I don’t really like this approach myself. But there it is. Just throwing it out there.

1 Like


I don’t know of any expression that can do this. Maybe there is but I don’t know.

But I can think of a simple solution you can try.

  1. Repeat for each number variable. If the numbers are in an array, you can use the repeat for each child variable event.
  2. Store the values in a new array: abs(number - 10).
  3. Sort the array using ArrayTools extension, this will sort it from lowest to highest number.
  4. The first child would have the smallest difference.
    So the closest number to 10 would be: newarray[0] + 10
1 Like

Instance vars were just example not what i want to do
I do not need it now i just see one day i will need it for something maybe even for something that does not involve objects

Yeah i did figure your solution before i made this topic
That is why i did ask is there a way to do it without extension in gdevelop
So seems there is no native way to do it
IDK why array tools are not integrated to gdevelop

It’s just an alternate way of doing that, considering as you say, there’s no native way to do it. It’s some out of the box kind of thing. Using objects to compute for something that doesn’t necessarily involve objects. But hey, I did say it was an ugly approach, didn’t I?

Besides, looks like yours and insein’s solutions are apparently a much, much better way to go at it. So yeah, I’ll just leave you to it then.

Actually there is even a simpler solution without the need for extension or a new array. I just thought of it now.

Here’s the event:

In the example, “array” holds the original 3 numbers that you’re comparing. After the repeat is over, the number closest to 10 would be: Closest + 10.

1 Like

@Levio’s object isn’t a bad option - create objects with x pos = value, y pos = 0, find position nearest to (0,0), store its x position and then remove the objects.

I’d use the min() function - create a variable (initially set to a ridiculously large number) to hold the smallest value. Loop through each value, setting the variable to the minimum of the two:


It can be ugly as hell as long as it works
I am just trying to avoid extensions

Yet there are many values i will need to check in the feature
Force Rotation Movement speed with pahtfinding or even positions

For example once i did this for another user to check 1st and 2nd and 3rd closest object to another object

It works it’s not elegant but it works
Yet that is for picking nearest object what if i need to check object that is moving fastest?
What if i need to check objects that spins to find one with most spins?
A lot use cases i see i would need to check in the future

@insein @MrMen
My example was for 3 numbers
Easy to manage
Imagine i need to check 100
Not all of them are in same group of objects or they are not even objects

I know always i am able to somehow add all numbers i want to check to array one way or another
Yet now i am sure there is no straight forward way to do that

I even no idea if i would be to ask for such check system how it should look
And how it should check

But whatever now i have some alternatives i am saving url to this topic
Yet i do not claim it as solved since maybe someone will have other idea which come in handy in different scenarios

Thank you all for your ideas

Have you tried the object picking tools extension.