How to use an inherited function signature

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

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.

:bust_in_silhouette: Reply From: scrubswithnosleeves

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.

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

wyattb | 2021-06-15 01:17

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().

scrubswithnosleeves | 2021-06-17 01:58