0 votes

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.

asked Jun 19 in Engine by MaaaxiKing (291 points)
edited Jun 19 by MaaaxiKing

1 Answer

0 votes

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

answered Jun 19 by rustyStriker (1,073 points)

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

in the input event:

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)

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

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

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

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

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

Unhandled input must be InputEventKey, mustn't it?

oh maybe? making the if useless... thanks
but the official docs check the type too

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 How to use this Q&A? before posting your first questions.