Hi. I'm adding some input events to a project. It works fine with key presses from the input map. Now I want to add some rudimentary touch controls, so I thought the best way would be to put this all into an _input function and handle each event type that I need, something like this:

func _input( event: InputEvent ) -> void:
	match event.type:
		KEY_EVENT:
			# handle key events
		TOUCH_EVENT:
			# handle touch events

	# any other event types can be ignored

The code above doesn't work. I don't know how/if I can get the event type. I tried typeof( event ) but all events return the same value. When I print event.as_text() I get results like these:

# InputEventScreenTouch : index=0, pressed=true, position=(1053.749878, 1044.749878)
# InputEventMouseButton : button_index=BUTTON_LEFT, pressed=true, position=(278, 594), button_mask=1, doubleclick=false
# InputEvent: InputEventMouseMotion : button_mask=0, position=(52, 192), relative=(2, -60), speed=(-78.997406, -1928.18457), pressure=(0), tilt=(0, 0)
# Right # <--- that's the input map value
# Left

I do have a working version that just checks key presses in _input with event.is_action_pressed( 'ui_left' ) etc. and uses invisible button nodes for touch controls, but I thought it'd be nice to have all the control logic in one place.

Hm, and as soon as I posted this I found a possible way, but I'm still not sure if that's the best method.

func _input( event: InputEvent ) -> void:
	if event as InputEventKey != null:
		# handle key events
	elif event as InputEventScreenTouch != null:
		# handle touch events

You would use the is keyword for this:

if event is InputEventMouseButton:

Oh, so simple! Thanks! I must've missed the keyword feature when I went through the GDScript.

Can't be used with match event:, but in this case that'd just be syntactic sugar, I can work with if blocks.

2 years later