Input event gives an error

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

The following code

func _input(event):
# If the user press a mouse button
if event.type == InputEvent.MOUSE_BUTTON and event.pressed:
if event.button_index == BUTTON_LEFT:

Gives throws the following error:

Invalid get index ‘type’ (on base: ‘InputEventMouseMotion’).

Please help

:bust_in_silhouette: Reply From: RedBlueCarrots

Rather than use event.type, try:

if event is InputEventMouseButton:

Place that outside the rest of the statement, though, because otherwise godot will try and check if moving the mouse is pressed, which doesn’t work

func input(event):
# If the user press a mouse button
if event is InputEventMouseButton:
    if event.pressed:
        if event.buttonindex == BUTTONLEFT
if event is InputEventMouseButton and event.pressed and event.buttonindex == BUTTONLEFT:
    print("this works too! because it only checks the second if the first true etc")

rakkarage | 2020-09-20 14:29