How to Fix Unexpected Behavior from the is_action_pressed Function

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

I was trying to use the is_action_pressed function to set a variable to a certain value as long as the action is pressed.
Weirdly enough it seems that this works for the first second or so when the key is pressed, but then continues as if the key is no longer pressed.

extends AnimatedSprite 

enter code herevar turn_speed = 1

var turn_dir = 0

var angle = 0

func _ready():
	rotation = 0

func _process(delta):
	angle += turn_dir * turn_speed * delta

func _input(event):
	if event.is_action_pressed("snake_turn_clockwise"):
		turn_dir = 1
	elif event.is_action_pressed("snake_turn_counterclockwise"):
		turn_dir = -1
		turn_dir = 0


I’d like to know what causes that problem and how I could potentially fix it.
Any help would be greatly appreciated.

:bust_in_silhouette: Reply From: klaas

you have to allow_echo on the InputEvent.

func _input(event):
    if event.is_action_pressed("snake_turn_clockwise",true): #<--- second parameter has to be true
        turn_dir = 1
    elif event.is_action_pressed("snake_turn_counterclockwise",true):
        turn_dir = -1
        turn_dir = 0

Thank you very much. That seems to have solved that. Though I’m encountering a new problem now.
When I press the other key while the sprite rotates to change direction, sometimes the rotation hiccups and takes a fraction of a second before starting to rotate in the other direction.

Any Idea for what might be causing it and how to fix it?

Lodea | 2020-07-22 11:20

Maybe its because of the If … elif … else construction, where in theory a event can be dropped.
As long as i hold clockwise, counterclockwise events will be ignored. But its possible to press both in the same time.

Maybe this may help

func _input(event):
    turn_dir = 0
    if event.is_action_pressed("snake_turn_clockwise",true): #<--- second parameter has to be true
        turn_dir += 1
    if event.is_action_pressed("snake_turn_counterclockwise",true):
        turn_dir += -1

klaas | 2020-07-23 09:31

Nope… Unfortunately, the issue persists :confused:

Lodea | 2020-07-23 10:25

There is a “gap” before echo happens …
check this out … works for me

func _process(delta):
	angle += (turn_left + turn_right) * turn_speed * delta
	rotation = Vector3(0,angle,0)

func _input(event):
	turn_dir = 0
	if event.is_action_pressed("snake_turn_clockwise"): 
		turn_left = 1
	elif event.is_action_released("snake_turn_clockwise"):
		turn_left = 0

	if event.is_action_pressed("snake_turn_counterclockwise"):
		turn_right = -1
	elif event.is_action_released("snake_turn_counterclockwise"):
		turn_right = 0

klaas | 2020-07-23 10:59

Yup, works perfectly now. I still don’t understand why this solution works and the previous one doesn’t though.

Lodea | 2020-07-23 11:11

its simple.

When you type and hold a key in an editor the first letter appears immediately, than after some milliseconds the echo begins and the key gets repeatet. This prevents that whenever you type a key multiply characters appear.

This works because it checks the first key press and release of the key and dont rely on repetitive events.

klaas | 2020-07-23 18:49

Well, I’m not sure I completely get it yet, but it’s good enough for now. Thanks, you’ve helped me a lot

Lodea | 2020-07-24 14:03