GameMaker Studio 2: Finding Top Most Drawn of All Instances at Mouse Cursor on Click

I need to make something that will return the top most instance where the mouse cursor is currently clicking. This is probably different in Studio 2, so how would I adapt the code below? It doesn't matter if the draw order is always the same, just that I can find from multiple instances at the mouse cursor which is drawing over all others. Here is the code I used to use, but I don't know this will actually get the top most instance as they might all be on the same layer, or depth in Studio 2. How do I change this to loop, and get the top most drawn instance at the mouse xy, using something like collision point probably?

Create event:

clicked_id = -4
clicked_object = "none"
clicked_top_depth=1000000000000000


Step Event:

//Gets the id of the top most clicked object at mouse xy cords
if mouse_check_button_pressed(mb_left)
{
clicked_id = -4
clicked_object = "none"
clicked_top_depth=1000000000000000
check=0

with (all)
{
if id != other.id
{
check=0
check = collision_point(mouse_x,mouse_y,id,true,false)
if check >0
{
//chosen only if depth is lower
if check.depth<=other.clicked_top_depth
{
other.clicked_id=check
other.clicked_object=object_get_name(check.object_index)
other.clicked_top_depth=other.clicked_id.depth
}
}
}
}
}

Draw GUI Event:


draw_text(0,0,clicked_object)
draw_text(0,30,clicked_id)
 
F

FB-Productions

Guest
Something i just whipped up quickly. It should all work as you wanted it to. Let me know if it doesn't. And if it does aswell! :)

Create event:
Code:
global.i = 0;
clicked_object = "N/A";
clicked_id = noone;
Step event:
Code:
if (mouse_check_button_pressed(mb_left))
{
     with (all)
     {
          if (collision_point(mouse_x,mouse_y,id,true,false))
          {
               global.check[global.i] = id;
               global.i += 1;
          }
     }
     
     var j = 0;
     var max_depth = 9999999999999999;
     while (j < global.i)
     {
          if (global.check[j].depth > max_depth)
          {
               max_depth = global.check[j].depth;
               clicked_id = global.check[j];
               clicked_object = string(object_get_name(clicked_id.object_index));
          }
          j += 1;
     }
}
Draw GUI event:
Code:
draw_text(0,0,clicked_object);
draw_text(0,30,clicked_id);
 
Top