How can I avoid is_action_pressed() from firing multiple times with joystick?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By NikOrin
:warning: Old Version Published before Godot 3 was released.

Hello, I’m not sure if this is the correct category, but I’m currently working on a dialogue display UI that also includes dialogue choices and came across an issue.

Basically, I currently have a list of buttons stored in an array list variable and I’m using event.is_action_pressed() to control which button is in focus.

if event.is_action_pressed('up') :
  if focus_button_index == 0:
	focus_button_index = choice_buttons.size() - 1
  else:
	focus_button_index -= 1
  choice_buttons[focus_button_index].grab_focus()

The problem I’m currently having is that the up action is on the joystick, and as I lean the joystick forward, it be considered “pressed” and “released” multiple times in one forward lean. Is there something wrong with my joystick or am I just using this function incorrectly?

In case the description was unclear, I’ll try to use angles to describe what the code would do:

With the joystick in the center (0 degrees): nothing happens.
I push it forward 10 degrees: nothing happens (still in joystick dead zone I assume)
I push it forward another 10 degrees (now at 20 degrees) without going back to the center: code enters if-statement
I push it forward another 10 degrees (now at 30 degrees) without going back to the center: code enters if-statement AGAIN

You can read the joystick in an analog way(which is the right way imo since joystick is an analog sensor) or you can use a timer/release mechanic to prevent this thing

rustyStriker | 2017-10-06 15:10

:bust_in_silhouette: Reply From: Ertain

Leave it to good ol’ Ivan Skodje to answer this question. When you press “up”, you have the code wait a few seconds, and check again. It’s something like “if the user is pushing up, assign a variable to keep track of whether they’re pressing up, then wait a few seconds, and check again”.

Hope that helps.