• 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 with multiple of the same object in a room

V

vincenttnecniv

Guest
Hello. I'm sure this is an easy fix, but I can't wrap my head around how instance id's work.
Im trying to select an object and then have it deselct that object when i click on another object, or anywhere else in the room. Right now, if I click on any of the objects in the room it selects the same object every time. I can click on any instance of the object, but only one will be selected. So if I had @ @ @ in a room, and I clicked on any of them only the far right @ would be selected every time. Any insight is greatly appreciated.
My code so far is
Create Event:
selected = false;
Step Event:
if (mouse_check_button_pressed(mb_left)){
var click_id = instance_position(mouse_x, mouse_y, all);
if(click_id){
//This is where my brain just refuses to function
}

if (position_meeting(mouse_x, mouse_y, obj_card)){
//This line is suppose to deselect any previous instance and then select the new one i click
obj_card.selected = false;
selected = true;
} else if (mouse_check_button_pressed(mb_left)){
selected = false;
}
}

Draw Event:
if (selected = true){
//This just draws an outline so I know if its working or not
draw_self();
draw_sprite(spr_selected, 0, x, y);
}
else{
draw_self();
}
 

TheouAegis

Member
if mouse_check_button_pressed(mb_left) {
with obj_card selected = false;
with instance_position(mouse_x, mouse_y, obj_card) selected = true;
}
 
D

DariusWolfe

Guest
It may not be the best way to do it, but I just have a global mouse event, with the first action being to deselect all instances of the object, and the second line being to select the instance at the mouse position (instance_position(mouse_x,mouse_y,obj_thing))

My actual code is designed to allow for other clickable objects down the road, and looks like the below:

obj_crewman.v_selected = 0
clicked = instance_position(x,y,all)
if clicked != noone
{
if object_get_name(clicked.object_index) = "obj_crewman"
clicked.v_selected = 1;
}

Down the road, when I need other clickable objects, I'll add more bits of code like the one in the "if clicked != noone" brackets.
 
Top