GMS 2.3+ Using the position of the mouse to trigger an event.

NotKrixxppi

Member
Hello everyone, I was looking for some assistance on a rather simple question, I was wondering if you could use the position of the mouse, to trigger an event. For one of my projects I would like to make it so whenever the player's cursor reaches a range of an (x, y,) coordinate that an event is triggered, for instance, travelling into another room. If this is not possible, I was also wondering if maybe this worked for objects, such as if a mouse hovers over a specific object, that an event is also triggered. Any feedback would help, thank you.
 

Liquid

Member
trigger an event is not the right word, you can code a workaround... similar to eventtrigger

Create an object, lets name it obj_zone_that_triggers, and you assign a fully transparent sprite of 3pixels x 3pixels and rezise it in room_editor for suiting size.
Now the trigger event: add event "mouse enter" and fill in your code. Done

Or better :add a variable that holds the script you wanna call and call script stored in the variable inside the "mouse enter" event
 

SoapSud39

Member
alternatively, without creating another object:
GML:
if mouse_x == target_x and mouse_y == target_y {
    //etc.
}
to check for mouse at position, but this would be too specific, so I would use point_in_circle or point_in_rectangle instead:
GML:
if point_in_circle(mouse_x, mouse_y, target_x, target_y, radius) {
    etc.
}
 

Liquid

Member
mouse enter will only execute once
but together with mouse exit event and one alarm event you can do the following:

continuious execution of code without permanent execution (inside any step event)
you can use:


EVENT mouse enter:
GML:
alarm_set(1,var_any_time_interval)

EVENT mouse exit:
GML:
alarm_set(1,-1)
//turn alarm off

EVENT alarm 1:
GML:
alarm_set(1,var_any_time_interval)
//AND ADD STUFF HERE that you like to execute all the time while mouse is over the zone obj
script_execute(my_predefined_script_assigned_in_room_editor,arg1,arg2,...)
 
Last edited:

NotKrixxppi

Member
I have not, but I know that event can only occur once, I'll give it a try to see if it helps with what I'm currently doing.
 
Top