How to get mouse input with 3.0 alpha build 1

memzenmemzen Posts: 16Member
edited October 2017 in Programming

I tried to get the mouse data in

func _input(ev):

with

if (ev.type == InputEvent.MOUSE_BUTTON and ev.button_index == BUTTON_LEFT and ev.pressed):

but as soon as the mouse is over the application window the app freezes and the Debugger says
'Invalid get index 'type' (on base: 'InputEventMouseMotion').'
So then I tried in

func _fixed_process(delta):

to use

if Input.is_mouse_button_pressed(BUTTON_LEFT):

and

mouse_xy = get_viewport().get_mouse_pos()

but the Debugger says
'Invalid call. Nonexistent function 'get_mouse_pos' in base 'Viewport'.'
So I don't know what to do. Thanks for any suggestions.


Tags :

Best Answer

  • memzenmemzen Posts: 16
    edited October 2017 Accepted Answer

    After much trial and error I found this code works.

    func _process(delta):
    if(Input.is_mouse_button_pressed(BUTTON_LEFT)):
    mouse_xy = get_viewport().get_mouse_position()
    g.mouse_x = mouse_xy.x
    g.mouse_y = mouse_xy.y
    print("Mouse position at: ",mouse_xy)

Answers

  • TwistedTwiglegTwistedTwigleg Posts: 1,413Admin

    Input events in Godot 3 has changed. You can see a couple examples on this thread that shows the new way of getting input.

    As for getting the mouse position, I think you can use get_mouse_position on the viewport. All of the previously named functions and variables that had pos have changed to position, which I think was causing the issue.

  • memzenmemzen Posts: 16Member
    edited October 2017 Accepted Answer

    After much trial and error I found this code works.

    func _process(delta):
    if(Input.is_mouse_button_pressed(BUTTON_LEFT)):
    mouse_xy = get_viewport().get_mouse_position()
    g.mouse_x = mouse_xy.x
    g.mouse_y = mouse_xy.y
    print("Mouse position at: ",mouse_xy)

Sign In or Register to comment.