0 votes

In the example below, given that event is a InputEventMouseButton, how can I code the signature to allow that type. In other words I want the func signature below:

func _input(event: InputEvent): 
    if event is InputEventMouseButton and event.is_pressed():
        target=(event as InputEventMouseButton).position
        pass

to look like this below ( but I get an error when I do this):

func _input(event: InputEventMouseButton): 
    if event is InputEventMouseButton and event.is_pressed():
        target=event.position
        pass

In this case InputEventMouseButton is a descendant of InputEvent and is also passed to the _input() func as such.

Godot version 3.3
in Engine by (805 points)

1 Answer

+1 vote
Best answer

Answering live on stream: https://www.youtube.com/watch?v=fC3384meGZA
should be at 1 hour 1 minutes

The following is working for me:

func _unhandled_input(event):
    if event is InputEventMouseButton and event.pressed:
        var target = event.position
        print(target)

You should not type hint virtual functions if that is what you are talking about.

by (597 points)
selected by

Yes, I am talking about parameter types of virtual functions. Without typing hinting you can't take advantage of the IDE's type hints.

Yeah, you should not type hint virtual functions where the parameters are variable like the _input() function. ie, the parameter is given to you by the engine and the type is variable, thus you cannot type hint it because you do not know what type you will be given. Everytime anything happens (the mouse moves, a key is pressed, etc.), it will register as an event in the _input() function. Thus you cannot type hint it except maybe as InputEvent.

If you want to type hint, you should do something like this:

func _unhandled_input(event):
    if event is InputEventMouseButton:
        var_event : InputEventMouseButton = event

If instead you are looking for a function that only monitors for one input type, then use _process() or _physics_process().

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.