GM:S 1.4 Clicking on Obj with greatest depth

Discussion in 'Programming' started by NukeyFox, Jun 8, 2019.

  1. NukeyFox

    NukeyFox Member

    Joined:
    Dec 18, 2016
    Posts:
    3
    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
     
  2. woods

    woods Member

    Joined:
    Jun 21, 2016
    Posts:
    213
    make the collision mask at the players feet?
     
  3. sp202

    sp202 Member

    Joined:
    Sep 26, 2016
    Posts:
    965
    Use collision_point_list, then loop through the objects in the list and find the one with the lowest depth.
     
  4. NoFontNL

    NoFontNL Member

    Joined:
    Feb 10, 2018
    Posts:
    258
    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: Jun 8, 2019
  5. samspade

    samspade Member

    Joined:
    Feb 26, 2017
    Posts:
    1,930
    You may also want to consider looking at this tutorial:

     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice