How to grap the mouse movement?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By bleistift

My goal is to move a node with the mouse, but without having a mouse pointer that can leave the window or is shown or can be stuck at the border of the window.

How this should work for the user:
The user starts the game and the mouse pointer dissappers. He could move the node by just moving the mouse. If he want to use other programs than the game, he has to pause the game, close it or switch to another window with ALT-TAB.

:bust_in_silhouette: Reply From: njamster

Attach this script to your node:

func _ready():
    # this line confines the cursor to the game window
	Input.set_mouse_mode(Input.MOUSE_MODE_CONFINED)

    # this line replaces the cursor image with a different image
    # that image can be 250x250 pixels large at max and (in our
    # case) is fully transparent to hide the actual cursor
	Input.set_custom_mouse_cursor(load("res://transparent.png"))

func _process(delta):
    # this sets the nodes position to the mouse position each frame
	globalposition = get_viewport().get_mouse_position()

Thank you very much! That works great!

bleistift | 2020-05-04 14:11

:bust_in_silhouette: Reply From: Ingeniou5

I managed to get my similar thing working with set_global_position(get_viewport().get_mouse_position())
Also you can change Mouse settings in a node to ignore it