Can I pick objects in a JavaScript code event for use in later events?

I can pick objects using various events, of course, but there are some things that there aren’t events for (for example, picking by lowest z-order). I can handle this by writing a JavaScript code event, but then I have to handle all the subsequent things in JavaScript too, when there are perfectly normal GDevelop events that can do the work I want. Can I alter the picked object list in a JavaScript code event and then have that object list continue on to later events?

For example, imagine that I have:

The cursor/touch is on _Slices_
    The text of variable _Activated_ of _Slices_ = "no"
-> action
Change the text of variable _Activated_ of _Slices_: set to "yes"

so mousing over a Slice sets a variable on it. That’s all good. However, in my game, lots of Slice objects overlap on the screen, and so mousing over a point where they overlap will activate all of them in that position. What I want to do is only activate the lowest one in the z-order. So ideally I want to do something like:

The cursor/touch is on _Slices_

      (function(runtimeScene, objects /*Slices*/) {
          // find the Slice with the lowest z-order
          let lowestSlice; let lowestZ = 999999;
          objects.forEach(slice => {
              if (slice.getZOrder() < lowestZ) {
                  lowestZ = slice.getZOrder();
                  lowestSlice = slice;
              }
          });
          runtimeScene.currentlyPickedObjectList = [lowestSlice];
      })(runtimeScene, objects /*Slices */);

        The text of variable _Activated_ of _Slices_ = "no"
-> action
Change the text of variable _Activated_ of _Slices_: set to "yes"

so my JavaScript can read the existing picked object list because it’s passed as objects, but then I want to set the existing picked object list so that that’s then passed on to the next event, which checks whether Variable(Activated) is set to “no”, and then my action will only affect the Slice which is lowest in the z-order.

Is this possible?

IDK if it’s possible to change the object’s picked status but what I would do is use the object’s Boolean variable. Set the Boolean variable to false for all of the instances before you compare the Z order and then set the lowest one’s variable to true. Then use a condition to check its value.

You can do the same without JavaScript by using the “for each object”.

Can I? How can I test that an object is lower z order than the other objects in a for each? It only lets me look at the individual object that’s currently in the loop, doesn’t it?

I like the Boolean trick, though, that’s neat! Hadn’t thought of doing it that way.

Off the top of my head. If you assigned each one an ID variable, you could save the lowest Zorder along with the matching ID. You could then use the ID as the check variable.

(Loose code)

lowestZOrder=9999999

for each object

object ZOrder < Variable(lowestZorder) | set lowestZorder = object.ZOrder
| set LowestZOrderID = object.Variable(ID)

Then to select the object
object.Variable(“ID”) = Variable(LowestZOrderID)

You could also just assign the ID as you went through the list using another variable like
| object.Variable("ID)=Variable(i)
| object.Variable("ID) + 1

Note: I used the | as a devider between condition and action. I forgot the site removes preceding spaces.

Use a variable to track the lowest z-order as you have in your javascript code with a Foreach object event. Then select the slice with the z-order that matches the variable’s value:

That last event will select the slice that has the lowest z-order, and you can apply actions on it accordingly.


[edit] Similar to @Keith_1357’s solution, but without the need for an ID.

1 Like

Yes, it’s definitely simpler. Just make sure each one has a unique Zorder. The benefit of the ID is that it picks the first one that’s lower. You could always just pick a random instance with the lowest Zorder.

1 Like

I think this extension can be a great source of inspiration:
https://wiki.gdevelop.io/gdevelop5/extensions/object-picking-tools/reference

2 Likes

Hi read here maybe helps