#Clicking the Area2D with the largest z_index under the cursor

6 messages · Page 1 of 1 (latest)

thick ingot
#

I have a scene with multiple Area2D objects that perform an action when clicked. Some of these objects partially overlap others, but the object that is clicked is currently based on the node's position in the tree. This is an issue when the positions and z_indexes of the objects change. I would like to have the clicked object be determined by the object with the highest z_index at the coordinates of the mouse cursor. I read online that this can be achieved by setting the physics_object_picking_sort of the viewport to true. I have attempted to do this by writing get_viewport().physics_object_picking_sort = true in the _ready() function of the scene's root node, but I did not notice a change in behavior. Am I accessing the viewport incorrectly, or should I be going about this a different way?

thick ingot
#

I have tried putting that line of code in each of the Node2D objects' _ready() functions, but I am still running into the same issue.

lost depot
thick ingot
#

The only way I can think of to do it for sure would be to create a collider at the location of the click, and return the colliding Area2D with the largest z_index, but that feels like the wrong way to do it.

lost depot