Select the item instance with the smallest(or highest) value of all instances of the same item?

How do I…

Select the item instance with the smallest(or highest) value of all instances of the same item?

What is the expected result

Have the next event only apply to the item instance with the smallest(or highest)

What is the actual result

nothing, because i can’t find out how to do so

Hello, lazyzy!

You didn’t explain exactly how these instances work in your game, but I found a logic that I believe can lead you to a specific solution:

I considered a solution using object variables too, but I was wondering if it is possible using basic object picking conditions/events. I suppose the easiest way would to just use a javascript event, I’ve been working on something like this. In the meantime I think your solution will work great.

Thank you for the anwser.
Tho i belive this would not work considering there is no threshold to hit in my case, as you can see on the image i have multiple tiles, each of them having different values stored, these values are results of formulas and so, are not simple coordinates, means i cannot use stuff simple as “closest to X”.
Let’s say i want the tile with the smallest g, i belive the only way to solve this is to directly anwser this question, or a way to store the smallest of all of these
fghd

Can you take the result of this formulas and store in a object variable to use like I showed?

they already are stocked as object variables

Are there more tiles? Or are you trying to select a tile out of the tiles shown in the screenshot (are they the same object)? I guess my question is, is your goal to select a specific object out of an object group that has the most instances, or is it to select an object(tile) from all the same tile objects that have the highest/lowest “g” value?

Edit: fixed many errors

I’m sorry but I don’t understand why it wouldn’t work. Can you explain in more detail?

there are more, and for each level of my game there will be a different number of them, at different places, my goal is to select the one instance of all of these tiles in the level that has the lowest value

So couldn’t you use a variation of Rasterisko’s example, perhaps using a formula that compiles the three values into one value that can then be tested against the others?

the number of tiles used and their placement will be different in every level and partially random, which makes the formulas highest and lowest kinda unpredictable, that is why i cant use a limit/trigger of 99999 or 0 or anything else, unless i manage to catch the instance with the lowest or highest value by some way

if i understand well, i still need a way to catch which of the instances has the smallest or highest value to adjust Rasterisko’s example

Oh I got. But you can just change this numbers…

Or do you think the result of your formulas still can ben highest or lowest of this numbers?

Correct me if I’m wrong, but wouldn’t picking each object using the for each loop, and then testing each object’s value (the compiled value of the three maybe) to find the lowest still work? Surely as Rasterisko said a ridiculously long number like 999999999 would be more than enough.

1 Like

what makes this impossible is that the values are dynamic and i will only know the values when i start the game/level, for exemple the first try will have values ranging from 0 to ~1000 and for the other will have values between ~200 and ~800…
So i tried your idea, and yeah let’s say i set highest at 900, but i am on a level where half of the tiles are over 1000 it will just delete all of these tiles and not only the one with the highest number, and the other way around, if i get tiles that doesn’t go over 600 it will never work

If you set the high/low variable to the object’s value without a condition or loop then it is going to get the value from the last object, although I don’t know how efficient that is. IDK if internally, it’s iterated though all of the objects or just picks the last. IDK if it would be better to pick a specific object. Although, even doing that probably causes Gdevelop to iterate through the objects to find the right one. Either way would be minimal and better than risking a value outside of the initial test value. I tested it using a custom expression and the debugger and it returned only the value of the last created object. This would guarantee at least one object was picked.

Sorry, lazyzy, but I don’t know if I get it again… :eyes:

Why set 900? Just define a huge number like I did to cover any result of your formulas, like 999999999999999 (if you are checking the lowest value) or -999999999999999 (if you are checking highest value)

Can you share screenshots of your events?

Here’s my events

Let me try to explain it another way:
the variable G, which is from the opentile object, is a formula, that is partially random, that means, i won’t know the exact value of the highest and lowest result value, before i get into my game level, and even in some level, it changes in real time. So that means i cannot have arbitrary thresholds such as 99 or 90 billions because it might work once but won’t work every other time.

Another exemple that leads to the same issue, let’s say i have 20 random values that returns a random letter from A to Z, and i want the first in alphabetical order, i can’t just say "give me the values that are A, because there are probabilities that none of the values have the A letter,(and the first alphabetical result being B or C or even more down the line) but there are also probabilities that multiple values have A.
In my case, replace A or Z by your highest or lowest value, maybe that can help making my issue more understandable

For text you’ll probably need to use JavaScript unless there’s an extension. For numbers you can use the previously mentioned method. Although for simplicity it would probably be easier to use the same function to choose the lowest or highest regardless of the data type.

The object picking tools extension would be a good framework to either modify or use as reference. I don’t think it would be too difficult to modify the z-order function to use an object variable.

For numbers, you can use the previous example.

What if you add a Boolean variable to the example. set it to false before the for each object and then as a subevent the for each object you add

If VariableName is false then set it to true and set the high or low value to some temp variable.

That would always use the first instance as the initial test value. (You would have to move the events inside the for each object event to a subevent of the for each object)

You can then use the temp variable to pick the object. If there were multiple matches then you could pick a random instance or use an additional criteria.

I think JavaScript is your best method.

I don’t know why you are putting just 400 in your scene variable if you know that your instance variables can have sometimes a number bigger than that. Why you don’t just change 400 to 999999999999999999 like I oriented before? Do you think your formulas still can have a number biggest than 999999999999999999?

For texts I don’t know how to help… :eyes: