GetGlobalMousePosition vs InputEventMouseButton.GlobalPosition

Godot Version

4.2.2

Question

Hello :slight_smile:
For an RTS game, I want to be able to drag a rectangle on the screen to select units. For this I capture mouse input and movement in a script attached to the main world node. This works fine if I use the GetGlobalMousePosition() function to get the start and end position for dragging the rect.
However, the global mouse position is not the same as (@event as InputEventMouseButton).GlobalPosition. Not only is it in another coordinate space (0,0 is top left corner fot the event) but also does the global mouse position lag. It always captures the position when the cursor is already on another space on the screen (which happens alot when dragging) while the event position is always accurate.

Can I transform the position of the event into the same coordinate system that GetGlobalMousePosition() uses and viceversa, so I can DrawRect() properly?

Or is there a better solution for my problem?

Thank you in advance!

CanvasItem.get_global_mouse_position() returns the position in the coordinate system of the Canvas Layer, that the CanvasItem is in.

The value of (@event as InputEventMouseButton).GlobalPosition depends on where you call that function (See the docs for details):

  • It could be either the coordinate system of the Canvas Layer, so that no conversion is necessary
  • Otherwise it could be in the coordinate system of the root viewport. (Under the assumption, that you only have a single viewport, you would need to transform coordinates between Viewport coordinates and the canvas layer coordinates)

So the answer to your question depends on where you call the function.