Input, how to detect pressing same (directional) button twice in a row?

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

I’m trying to have a character do a dash move by pressing twice in directional pad (so I guess a “press, release, press” logic), but cannot seem to have the logic right.

I have some code like this:

func _process(delta):

    var move_right = Input.is_action_pressed("move_right")

    // here I tried some logic to detect player pressed twice in the directional pad
    (...)

    if(dash):
         // do dash move
    elif(move_right):
        player.set_scale( Vector2(1, 1) )
        player.move_local_x( player_speed * delta )

I tried using is_echo() in the _input()
Also using is_action_released()
Also tried storing the previous action press (or previous action release) in a variable.
Just can’t seem to get it right.

It either did not work or the character would just dash by holding directional pad right.

:bust_in_silhouette: Reply From: vinod

This is not a Godot specific question, but one which can be applied to any tech.
I’ll answer anyway.

  1. Declare a timer variable with a value, may be 0.5
  2. In the process function, you should reduce it by delta
  3. Reset the timer to 0.5 on key press and also if is_echo is false.
  4. Then check if the timer is greater than zero when the key is pressed

That should do it

Thanks for the help!

cardoso | 2016-05-28 17:35

An alternative way would be to store the current time and compare to the last, so you dont need to put anything in _process().

Zylann | 2016-05-29 10:36

Another option would be to use a Timer node + timeout signal

cardoso | 2016-06-04 15:18