1. Hey! Guest! The 35th GMC Jam will take place between November 28th, 12:00 UTC - December 2nd, 12:00 UTC. Why not join in! Click here to find out more!
    Dismiss Notice

Selecting one object with multiple of the same object in a room

Discussion in 'Programming' started by vincenttnecniv, Oct 7, 2016.

  1. 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();
    }
     
    VentKazemaru likes this.
  2. TheouAegis

    TheouAegis Member

    Joined:
    Jul 3, 2016
    Posts:
    7,033
    if mouse_check_button_pressed(mb_left) {
    with obj_card selected = false;
    with instance_position(mouse_x, mouse_y, obj_card) selected = true;
    }
     
  3. DariusWolfe

    DariusWolfe Member

    Joined:
    Sep 23, 2016
    Posts:
    32
    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.
     

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