Getting Mouse X and Y in GDScript?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By The_Duskitty
:warning: Old Version Published before Godot 3 was released.

Just need to know how to do this cause i cant seem to find anything on it in the documentation

:bust_in_silhouette: Reply From: lukas

Do you mean position of mouse input event?

func _input(event):
    if event.type == 2 or event.type == 3:
        # Mouse motion or mouse button event
        var mouse_event_pos = event.pos 

You can also use these constants for clarity:

if event.type == InputEvent.MOUSE_MOTION or event.type == InputEvent.MOUSE_BUTTON:

Zylann | 2016-03-13 22:41

Not only for clarity, since APIs tend to change over time… :slight_smile:

Bojidar Marinov | 2016-03-14 10:28

:bust_in_silhouette: Reply From: zendorf

If you are creating a 2d game, you can get access to mouse x/y co-ordinates inside any function by using get_global_mouse_pos().

for example:

var mouse_pos = get_global_mouse_pos()
var mx = mouse_pos.x
var my = mouse_pos.y

This is the Most Helpful one, thanks ^^

The_Duskitty | 2016-03-15 01:28