SOLVED Clamp mouse_x mouse_y in a range

Sawyer

Member
Hello everyone,

I did a teleport range in my little project.
If mouse_x and mouse_y is > max_range so i stop to display the cursor, and i draw a sprite at the coordinate i want using lengthdir.

GML:
var x_desti = mouse_x
var y_desti = mouse_y
direction = point_direction(x, y, mouse_x, mouse_y);
if distance_to_point(x_desti, y_desti) > max_range
{
    x_desti = obj_player.x + lengthdir_x(max_range, direction)
    y_desti = obj_player.y + lengthdir_y(max_range, direction)
}
        
if distance_to_point(mouse_x, mouse_y) < max_range
{
    cursor_sprite = spr_crosshair
}
else
{
    cursor_sprite = noone
    draw_sprite(spr_crosshair, 0 , x_desti, y_desti)
}
My problem is that i also want to clamp mouse_x and mouse_y in the max_range to avoid my mouse going too far from x_desti y_desti.
The problem is that mouse_x and y are read only variable.

Can you help me?
 

Joe Ellis

Member
You can use window_mouse_set, or display_mouse_set.
They literally change the position of mouse_x & y.
The only problem is that it does it at the start of the next step\frame. It's something to do with the mouse coordinates being a built in variable in windows or the OS itself, so gm gets the "environment variable" at the start of the step, then sets the readonly mouse_x&y. So to change the actual mouse coord variable it has to interact with the os, then it won't get the result until the next step when it gets the environment variable again.
It would be good if gm allowed the mouse_x&y to be changed after it's got the coords from the os, cus it'd make things like this less awkward.
Cus you wouldn't have to wait until the next frame to use mouse_x&y after you've asked for their coordinates to be changed.
But you can do that by making 2 variables that are the new mouse coords, and use those accordingly instead of mouse_x&y, then don't reference mouse_x&y after calling window_mouse_set during that step, use the 2 "imitator" variables instead.
 
Last edited:
Top