What is the difference between get_global_mouse_position and event.global_position?

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

Hi,

A quick overview - I have a sprite that uses the Area2D signal _input_event to detect it has been clicked and then highlight grid tiles on a tilemap. I want to then click one of those highlighted tiles and move the sprite there, which is where the problem arises.

I am retrieving the global mouse coordinates when a mouse depress is detected and then using world_to_map to obtain the tilemap coordinates . Using _input(event)on my sprite script I can obtain event.position or event.global_position and that works fine until I change the camera position at which point I get the wrong tilemap coordinates.

I have tried using get_global_mouse_position and this works well even when the camera moves. However, using this method means that it will detect where the mouse last was rather than the click event, so if I move the mouse quickly after clicking it can give a false result. So my question is; what is the difference between using event.global_position and get_global_mouse_position in my case? Is there a better way to do this?

My scene structure is as follows:

Node2D (Root)
 -Camera2D
 -Tileset (parent)
   -Sprite (child)

Thanks

:bust_in_silhouette: Reply From: Andrea

event.global_position gives you the position of the event that triggered the _input(event) function. It can be any kind of event, even mouse movement itself is an event., or a key stroke.
get_global_mouse_position() will instead give the position of the mouse position indipendently from any input.

if get_global_mouse_position works in your project, you can combine it with _input(event) so that it only triggers at the right moment.

func _input(event):
  if event.is_action_released("left_mouse"):
    var pos=get_global_mouse_position()

Or you could to some math and calculate what you need from the event.global_position and the translation/rotation/zoom of the camera

Hi, Thanks for the answer. Still a little confused on the real difference between using event.global_position vs get_global_mouse_position? See below: should these be the same?

func _input(event):
if event is InputEventMouseButton:
	if not event.pressed:
    #both print the same until camera zooms out, then both print different
		print ("get_global_mouse_position" , get_global_mouse_position())		
		print ("event.global_position" , event.global_position)		

McVities Chocolate | 2020-05-27 14:02

no, in detail

  • get_global_mouse_position(): returns the position of the world below the mouse, this is affected by camera zoom (because greater the zoom, greater the world coordinate span).
  • object.get_local_mouse_position(): same as above, but the position is relative to the object (if the object is the camera, the center of the screen is always (0,0)). Basically is the same as doing get_global_mouse_position()-object.position
  • event.global_position returns the pixel coordinate of the event on the window (top left of the windows is always (0,0), bottom right corner is (window width,window height). Unaffected by camera position and zoom
  • event.position return the position of the mouse relative to the viewport. In normal cases the viewport=window screen, but you can modify the viewport size and stretch it. For example if you set the viewport.size=Vector2(100,100), the event.position will return (0,0) on the top left corner, and (100,100) on the bottom right.

Andrea | 2020-05-27 16:18

Great, that clears everything up. Thank you!

McVities Chocolate | 2020-05-27 23:08

I signed up to thank you for this :). Been looking everywhere including the docs, yet still found nothing but this. Never thought QnA would be more helpful than documentation, until now

Amuh | 2020-10-15 18:55