has() function not working to check for class

Godot Version

Godot v4.3.stable

Question

I’m creating a system to accept both controller and keyboard+mouse inputs, and change things like UI accordingly. However to do this, I need to check if an input is from a joypad or k+m, and so I don’t have a big if/else script, I’m using the following code:

extends Node
signal input_mode_changed


enum InputModes {
	KEYBOARD,
	CONTROLLER
}

var current_input_mode := InputModes.KEYBOARD :
	set(new_input_mode):
		current_input_mode = new_input_mode
		input_mode_changed.emit()
var keyboard_inputs := [InputEventKey, InputEventMouse]
var controller_inputs := [InputEventJoypadMotion, InputEventJoypadButton]


func _input(event: InputEvent) -> void:
	if keyboard_inputs.has(event.get_class()):
		current_input_mode = InputModes.KEYBOARD
	else:
		current_input_mode = InputModes.CONTROLLER

However, even when the event class is contained by the keyboard_inputs array, it never returns true, so the input mode is always CONTROLLER. I know that for this an else/if could work without much problem, but I want to at least understand why it does not work.

try to check class just by

func _input(event: InputEvent) -> void:
	var is_keys:=false
	if event is InputEventKey: is_keys=true
	elif event is InputEventMouse: is_keys=true
	if is_keys: current_input_mode=InputModes.KEYBOARD
	else: current_input_mode=InputModes.CONTROLLER

Have you tried to print(event)?
try it and you’ll understand

1 Like

It works to use if and else. I just can’t understand why the has() function didn’t work. I tried to use print(event.get_class()) and it did match with the classes in the arrays. Then, i tried to use has(InputEventKey) in my keyboard array and it did work. It just doesn’t when i try to use both.

get_class() returns a string.
You don’t have any string values in your array.