Object picking with highest z order - how can I instruct it to only pick one object?

Hello, I am currently making an archaeology game that relies on the z order of objects to dig. Objects include both dirt tiles and artifact sprites, which I have put into a group I call “Ground.” I am using the object picking extension to choose items with a higher z order to dig through, but I have found that it isn’t very reliable and will sometimes pick more than one instance. The idea is that when I click on an instance, it will be deleted and reveal whatever instance is underneath, but right now my problem is that both the intended instance and the instance below it are being deleted. If the artifact is smaller than the dirt tile, then it seems to be ok, but if it’s a larger artifact then it is impossible to uncover.

I am rather new at making games, so I appreciate any help I can get. I’ve attached a screenshot of the events related to this.

Try this, it should work for object groups too.

The object picking extension is going to the most efficient/performant way of picking the object. And unless something has changed, It is accurate too.

It would only pick multiple objects if you have a bunch of objects with the same z order (and therefore they all are the highest z order).

I would add the “pick first” condition underneath your current “pick highest zorder” condition, or if it doesn’t matter which one, asd the “pick random” condition underneath it.

Either way it will pick only a single result from your filtered object list.

Thank you both for the suggestions. Unfortunately, neither of those solutions fixed the problem. Could the issue be something other than the z-order?

It could be something else. Can you share more details. Maybe a few pics or a video/gif of the issue? Maybe a screenshot of how you implemented the suggested events. The order of events matter. We probably need a larger picture of the issue.

So I have 4 layers of dirt panels. The left screenshot is the top dirt layer and is what you see when you open the game. The right screenshot is the layer right underneath the top layer and should be revealed as you clear away the top layer. The artifacts are their own objects, but are in the same group (“Ground”) as the dirt tiles, so they shouldn’t behave differently than the dirt. I have tried putting the artifacts on the same layer as the dirt as well as putting them on their own layer sandwiched in-between dirt, but that doesn’t seem to matter.

I opened the debugger while testing it and at the start, it showed the artifact objects were present and then after I dug for a while, the debugger said they were deleted, so I figured what happened was I was digging through them. But shouldn’t I have been able to see them first before destroying them?

As for events, there isn’t really anything related to this besides for the screenshot I inserted into the original post. The only thing before that is to instruct the Digging variable to change to “true” when a toggle switch is activated.

Make sure you have the most recent version of Gdevelop. There was an issue with collision masks.

I did a little testing and the picking behavior never picked more than 1 object for me. I’m not saying it isn’t for you. I’m just saying it didn’t for me. I tested it by having it assing the objects in the group to random layers and z order.

You can test it yourself using the number of picked instances and either use the debugger or a text object to view the count.

Things to check. Make sure the objects all have different z order values in the editor. If they share the same value, only 1 sprite can be on top but the behavior wouldn’t know which one was really on top. It would probably just pick either the first instance or the last one.

Check the collision masks. The masks don’t need to be perfect but objects are clicked on and picked by the mask not the image. So, if there’s an oversized mask then the objects can overlap even if they don’t look like it.

1 Like

Just to check. When you say “Layers”, you don’t mean actual scene layers, right?

Scene layers each have their own Z orders and are wholly unrelated to one another. As far as I know the pick highest z order condition doesn’t know/care what layer the objects are on, so if there’s conflicting z orders on different layers that could be issues.

(That said, the above does not explain whatever is causing you to pick multiple instances.)


Ok wow I had just assumed that the scene layers rearranged z order as well since the layers above will still appear over the layers below. But yes, that was the issue. The artifacts had a higher z order than the dirt so I was destroying them even though I couldn’t see them.

Thank you all so much for your help!!!