Checking Which Object Has Been Clicked

Discussion in 'Programming' started by TheBaileyAce, Jun 26, 2016.

  1. TheBaileyAce

    TheBaileyAce Member

    Joined:
    Jun 22, 2016
    Posts:
    6
    Hey everyone,
    I'm trying to figure out how to click any object in the game and have it return the name of the object I am clicking.
    Does anybody have any ideas?
     
  2. PlayLight

    PlayLight Guest

    You can use object_index to return the object index that the calling instance has been created from.
    Code:
    if ( mouse_check_button_pressed( mb_left ) )
        {
        show_message( object_get_name(object_index) );
        }
    
     
  3. TheBaileyAce

    TheBaileyAce Member

    Joined:
    Jun 22, 2016
    Posts:
    6
    Is there a way to use this, or something similar on just a player object and still have it tell me the name of any object in the game that I click?
     
  4. PlayLight

    PlayLight Guest

    In the player object you can use instance_position( x, y, obj );
    For example:

    Code:
    if ( mouse_check_button_pressed( mb_left ) )
        {
        with ( instance_position( mouse_x, mouse_y, all ) )
            {
            show_message( object_get_name(object_index) );
            }
        }
    However this will cycle through all instances within the room, so if you have a lot of instances running you may want to use parenting on the selectable objects, and replace 'all' with the parent object.
     
    Last edited by a moderator: Jun 26, 2016
  5. TheBaileyAce

    TheBaileyAce Member

    Joined:
    Jun 22, 2016
    Posts:
    6
    Thank you :D It works great!
     
  6. PlayLight

    PlayLight Guest

  7. TheBaileyAce

    TheBaileyAce Member

    Joined:
    Jun 22, 2016
    Posts:
    6
    Sorry to kinda re-open the issue, but is there a way to find out the specific instance of the object that is clicked?
     
  8. wamingo

    wamingo Member

    Joined:
    Jun 21, 2016
    Posts:
    57
    you want to id I think. instance_position returns the id.
     
  9. PlayLight

    PlayLight Guest

    wamingo is right, so in your case if you want the id of the obect you clicked on it would be:
    Code:
    if ( mouse_check_button_pressed( mb_left ) )
        {
        with ( instance_position( mouse_x, mouse_y, all ) )
            {
            show_message( string(id) );
            }
        }
    of course you can use the id for whatever you require, above i'm just displaying it in a message for debug purposes.
     
    Last edited by a moderator: Jun 26, 2016
  10. TheBaileyAce

    TheBaileyAce Member

    Joined:
    Jun 22, 2016
    Posts:
    6
    Is there a way I can find the x and y values of the object id? I've tried id.x and id.y but haven't had much luck.
     
  11. NightFrost

    NightFrost Member

    Joined:
    Jun 24, 2016
    Posts:
    1,948
    That should work, because instance_position returns the id value of the instance. You should be able to, for example:
    Code:
    Id = instance_position(mouse_x, mouse_y, all);
    if(Id != noone){
        show_message(Id.x);
    }
    Edit - oh right, don't use lowercase "id" because that explicitly refers to the id of the currently running instance. Always capitalize your variables to avoid collisions with reserved variables and the like.
     
  12. hippyman

    hippyman Member

    Joined:
    Jun 20, 2016
    Posts:
    565
    No no no..

    instance_position returns the id of the instance you're clicking.
    for example

    Code:
    var objectClicked = instance_position(mouse_x,mouse_y,Object);  //this will either store noone or the id of the instance the mouse is on
    
    objectClicked.x = whatever you want
    
    objectClicked.varName accesses variables local to that instance
    every instance has an object_index variable which tells what type of object it is.
    which you then use object_get_name() to get the actual string value of the object type.

    There's everything you're asking for :)
     
  13. PlayLight

    PlayLight Guest

    The OP is looking to click on any instance in the room, which is why you either use all or a parent object for the selectable objects.
    Using a with statement will reduce additional checking needed for unknown object errors.
    You can retrieve any built-in variables from the selected instance within the with statement.

    Code:
    if ( mouse_check_button_pressed( mb_left ) )
        {
        with ( instance_position( mouse_x, mouse_y, all ) )
            {
            var info;
            info        = object_get_name(object_index);
            info        = info +"#Pos X: " +string(x);
            info        = info +"#Pos Y: " +string(y);
            show_message( info );
            }
        }
    Or you can return the information back to the 'Player Object':
    Code:
    if ( mouse_check_button_pressed( mb_left ) )
        {
        with ( instance_position( mouse_x, mouse_y, all ) )
            {
            var info        = "";
            info            = object_get_name(object_index);
            info            = info +"#Pos X: " +string(x);
            info            = info +"#Pos Y: " +string(y);
            other.details   = info;     // must be initialized in the player objects create event.
            }
        }
    whatever you like really.
     
  14. TheBaileyAce

    TheBaileyAce Member

    Joined:
    Jun 22, 2016
    Posts:
    6
    Thanks guys, I've combined several ideas from this thread and have something which works fairly well. I just need to tinker with some numbers and stuff and it should be all good :)
    Thanks for the help, everyone :)
     

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