So I have my Q and E set to control a Camera that is fixed in 8 directions. The problem is when I call Input.is_action_just_pressed() it sets true two times, so it does its content twice.
This is what it does with the counter:
0 0 0 0 1 1 2 2 2 2
How can I fix thix?
if Input.is_action_just_pressed("camera_right", true):
if cardinal_count < cardinal_index.size() - 1:
cardinal_count += 1
else:
cardinal_count = 0
emit_signal("cardinal_count_changed", cardinal_count)
_process
or _physics_process
Your code should work correctly - without reporting twice - if it is running in _process
or _physics_process
.
This is because is_action_just_pressed
will return if the action was pressed in the current frame. By default that means graphics frame, but the method actually detect if it is being called in the physics frame or graphic frame, as you can see in its source code. And by design you only get one call of _process
per graphics frame, and one call of _physics_process
per physics frame.
_input
However, if you are running the code in _input
, remember you will get a call of _input
for every input event. And there can be multiple in a single frame. Thus, there can be multiple calls of _input
where is_action_just_pressed
. That is because they are in the same frame (graphics frame, which is the default).
Now, let us look at the proposed solution (from comments):
if event is InputEventKey:
if Input.is_action_just_pressed("camera_right", true) and not event.is_echo():
# whatever
pass
It is testing if the "camera_right"
action was pressed in the current graphics frame. But it could be a different input event that one being currently processed (event
).
Thus, you can fool this code. Press the key configured to "camera_right"
and something else at the same time (or fast enough to be in the same frame), and the execution will enter there twice. Which is what we are trying to avoid.
To avoid it correctly, you need to check that the current event
is the action you are interested in. Which you can do with event.is_action("camera_right")
. Now, you have a choice. You can do this:
if event.is_action("camera_right") and event.is_pressed() and not event.is_echo():
# whatever
pass
Which is what I would suggest. It checks that it is the correct action, that it is a press (not a release) event, and it is not an echo (which are form keyboard repetition).
Or you could do this:
if event.is_action("camera_right") and Input.is_action_just_pressed("camera_right", true) and not event.is_echo():
# whatever
pass
Which I'm not suggesting because: first, it is longer; and second, is_action_just_pressed
is really not meant to be used in _input
. Since is_action_just_pressed
is tied to the concept of a frame. The design of is_action_just_pressed
is intended to work with _process
or _physics_process
, NOT _input
.