+1 vote

I have a problem in input detection.
When I pressed a key (for example, left key), the input correctly detected that the key is pressed, but when then I pressed another key (for example, up key) without releasing the previous key, the input only detected the up key. The left key was only being detected again when I released the left key.
Did I do something wrong? I'd tried to print and see what the input detected, and I was right. Here is the input script I'm using:

func _input(event):
    if event.is_action("ui_left"):
        #go left
    elif event.is_action("ui_right"):
        #go right
    if event.is_action_pressed("ui_up"):

I had also tried to change the script by separating the up key detection in another node, but still having the issue I said in the beginning.
Sorry for my bad English :)

in Engine by (61 points)

Thank you so much! Now the problem has been solved :)

1 Answer

+5 votes
Best answer

_input is only called once per input event, and the event variable will only concern that particular input. This is why you receive left and up individually.

If you want to check both, you should remember the key state in a boolean variable, or use Input.is_action_pressed("ui_right"), or check input every frame in _process.

by (28,966 points)
selected by

The only problem I see with that is that checking every frame is kind of bumpy, and even in that case the inputs will be checked in every loop in a certain order, so interference is to be expected.

I also found that using elif in fixedprocess doesn't allow two keys to be active at once, only works with if alone. Which is weird, since it wasn't like that in previous versions. :|

Using elif will check only one of the two branches, because that's how the programming language is built (and every other as well).
If you want to check multiple keys, just write if lines and they will be checked one by one regardless of the state of the others:

if key_1:
    do stuff
if key_2:
    do stuff

Also it's fine to check every frame, that's how many games do to move characters in real time. You can do it with _input by managing a set of booleans but in the end you need to convert them into movement anyways.

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.