• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Selecting one object from multiple instances

D

Darren

Guest
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
 

Rob

Member
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:

FrostyCat

Redemption Seeker
Mouse Left Button Pressed event:
Code:
if (selected) {
  selected = false;
} else {
  with (obj_wall) {
    selected = false;
  }
  selected = true;
}
 
D

Darren

Guest
Mouse Left Button Pressed event:
Code:
if (selected) {
  selected = false;
} else {
  with (obj_wall) {
    selected = false;
  }
  selected = true;
}
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)
 
Top