Selecting one object from multiple instances

Discussion in 'Programming' started by Darren, Jan 10, 2019.

  1. Darren

    Darren Member

    Joined:
    Jun 25, 2016
    Posts:
    105
    I'm making a city builder, if I click on a wall piece I'm going to display its health and also for example destroy that wall piece. When the player clicks on it, I wish for it to become 'selected' and then it will be highlighted, I have set this up but what I want is if the player then clicks something else, it will select that, and unselect the former object.

    If I write "with obj_wall (selected = false;)" then it does it for the object itself also. I've checked the manual and tried to understand the id function but I can't get it to work.

    I need a simple code that says if I click on one instance of the wall, it is selected, if I click on another, that wall is selected instead and unselect the previous one. I also want to unselect the current selection if it is clicked again. Any help on this is much appreciated as there will be hundreds of the exact same instances and I could do with wrapping my head around this!

    Thanks all
     
  2. Rob

    Rob Member

    Joined:
    Jul 12, 2016
    Posts:
    508
    When I've done this myself, I always checked for a mouse click inside the object itself and then run this:

    Code:
    //Check if mouse is over this instance and if the mouse is clicked run this code:
    
    
    //Assuming your sprites are centered in the middle
    x1 = x - (sprite_width/2);
    y1 = y - (sprite_height/2);
    x2 = x + (sprite_width/2);
    y2 = y + (sprite_height/2);
    
    if (collision_rectangle(x1, y1, x2, y2, obj_mouse, false, false)){
    
       if (mouse_check_button_pressed(mb_left)){
          with (obj_wall){
             selected = false;
          }
    
          selected = true;
       }
    }
    
    That's one way and you'll need a new object - obj_mouse, and you just tell it to set its x to mouse_x and y to mouse_y
     
    Last edited: Jan 10, 2019
    Darren likes this.
  3. FrostyCat

    FrostyCat Member

    Joined:
    Jun 26, 2016
    Posts:
    3,416
    Mouse Left Button Pressed event:
    Code:
    if (selected) {
      selected = false;
    } else {
      with (obj_wall) {
        selected = false;
      }
      selected = true;
    }
     
    Darren likes this.
  4. Darren

    Darren Member

    Joined:
    Jun 25, 2016
    Posts:
    105
    Perfect, thanks. Sorry to add another question, I'll make another topic if it's too much to ask but if I then have a GUI object with the details of the building when it is selected, and I want it to go away when nothing is selected, how do I go about doing that?

    When I tried

    with obj_solid
    {
    if selected != true
    instance_destroy(obj_building_info,false);
    }​
    It has the issue of most of them being not selected, but one of them being selected, and it endlessly deleted and recreates the object. I just want it do go away if none of the objects are selected. (obj_solid is the parent of all my building objects)
     
  5. FrostyCat

    FrostyCat Member

    Joined:
    Jun 26, 2016
    Posts:
    3,416
    See: with block recipe cards (If at least one instance satisfies a condition)

    Also, it's clear from the way you write that you do not understand the difference between objects and instances. Read this article.
     

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