I found get_global_mouse_position()
to be insufficient in certain cases, i.e. when you wanna make an object (Light, Sprite) follow the cursor AND ProjectSettings.display/window/stretch/aspect
is set to expand
AND the window is resized. The object will then be misaligned.
So if you have a Light or Sprite or other Node2D that has an offset
property, you can compensate for this effect like this:
position = get_global_mouse_position()
offset = get_local_mouse_position()
I assume the reason is that a global transformation is implicitly applied that results in a change of the local position (you can check by debugging get_global_transform()
and realize that get_global_transform().xform_inv(get_global_mouse_position()) == get_local_mouse_position()
)