Checking Which Object Has Been Clicked

T

TheBaileyAce

Guest
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?
 
P

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) );
    }
 
T

TheBaileyAce

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) );
    }
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?
 
P

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

TheBaileyAce

Guest
In the player object your 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.
Thank you :D It works great!
 
T

TheBaileyAce

Guest
Sorry to kinda re-open the issue, but is there a way to find out the specific instance of the object that is clicked?
 
P

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

TheBaileyAce

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.
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.
 

NightFrost

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

hippyman

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

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.
 
T

TheBaileyAce

Guest
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 :)
 
Top