How can I check if the user has pressed a key that depends on a random variable

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

I want to randomly generate a letter or a number (a-z, A-Z, 0-9) and all the chance for every letter/number is equal. Then I want to check if the player has pressed the right key (the one with the random var) and if so, something should happen. All other keys except Space should be ignored.

:bust_in_silhouette: Reply From: rustyStriker

You can pick a random key_code and check in the _input callback if the key_code equals to the generated one

I don’t understand anything xD Could you maybe show me how?

MaaaxiKing | 2020-06-19 17:43

in the input event:
Using InputEvent — Godot Engine (stable) documentation in English

you can check if the pressed key equals to the generated key code

do note that they are usually enums and if im not mistaken godot translate enums to ints by default(please correct me if wrong)

rustyStriker | 2020-06-19 18:34

I made something but on my phone it doesn’t work. Please look here, maybe you can find the problem.

MaaaxiKing | 2020-06-19 18:36

touch events doesnt have a is_pressed function on them, so it disregards it and moves on(probably throws an error in the debugger)

rustyStriker | 2020-06-19 18:44

func _unhandled_input(event: InputEvent) -> void:
	if event is InputEventKey and event.scancode == KEY_ALT:
 		pass

print((randi() % 10) + KEY_A)

rakkarage | 2020-06-19 18:45

No, because sometimes when I press the right key, it does what it should if the right key is pressed :frowning:

MaaaxiKing | 2020-06-19 18:46

Unhandled input must be InputEventKey, mustn’t it?

MaaaxiKing | 2020-06-19 18:56

oh maybe? making the if useless… thanks
but the official docs check the type too
Using InputEvent — Godot Engine (stable) documentation in English

rakkarage | 2020-06-21 14:56