Legacy GM Clicking on Obj with greatest depth

NukeyFox

Member
I'm making an isometric game, so some sprites overlap.
upload_2019-6-8_21-23-30.png
The red blocks are placeholders for the playable characters.
I can click on the red blocks and it will store the id in a global variable global.player_selected.
The depths of the red blocks are set by depth = -y

However, when clicking overlapping sprites, e.g. the middle one in the picture, it selects the one behind it.
Is there a way to select the red block that is most in-front (with greatest depth)?

Most ideas I had didn't work out, so I'm currently drawing a blank right now.
Thanks
 

sp202

Member
Use collision_point_list, then loop through the objects in the list and find the one with the lowest depth.
 

NoFontNL

Member
This code will help, however you need this script to make it function.
https://pastebin.com/raw/M7R7mhLW

It is a modification of https://www.gmlscripts.com/script/instance_place_list

When using GMS2, you should rename the script.

Code:
/// Code when clicking
var list = collision_point_list(mouse_x,mouse_y,obj_playerObject,-1,1);
var inst = noone;

if(list!=noone){
      for (var i = 0; i<ds_list_size(list); i++){
           if(inst==noone){
                 inst=ds_list_find_value(list,i);
                 continue;
           }
           if(ds_list_find_value(list,i).depth<inst.depth){
                 inst=ds_list_find_value(list,i);
           }
      }
}
global.player_selected=inst;
ds_list_destroy(list); // Prevent memory leak.
 
Last edited:
Top