How to move cursor in and out of game window with cursor sprite?

Basically, I would like to be able to move my mouse in and out of the game window freely, and to hide the mouse cursor when inside the game window and instead showed a ‘cursor sprite’.

Window.set_mouse_lock(true) work’s fine, but it stops me from moving my cursor outside the window, for example to drag or close the game window. I’ve added a little code to check if action.screen_x & y is within the game window, and to set mouse lock to false if not. However, I noticed it caused the mouse to ‘jump’. E.g. if I move the mouse from top of computer screen to the bottom, when it move past the bottom of the game screen, the mouse jumped back to the top of the game screen. Is there a way to set mouse position through script to mitigate the effect or have I went about this the wrong way?

function on_input(self, action_id, action)local cursor_within_window
	if action.screen_x < 0 or action.screen_x > self.window_wd or 
	action.screen_y < 0 or action.screen_y > self.window_ht then
		cursor_within_window = false
	else
		cursor_within_window = true
	end

	if cursor_within_window ~= self.cursor_within_window then
		self.cursor_within_window = cursor_within_window
		window.set_mouse_lock(cursor_within_window)
	end
end

Might be possible using this:

defos.set_cursor_pos(x, y) -- In screen coordinates
defos.set_cursor_pos_view(x, y) -- In game view coordinates
2 Likes

That works, thanks!