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.

in Engine by (381 points)
edited by

1 Answer

0 votes

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

by (1,204 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 Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.