How to detect when an action is double pressed and hold on second press.

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

Hello, it sounds confusing since I don’t know how to describe it well. You know those games that when you hold down an arrow key it’ll make the player walk but when you press, release, and hold that arrow key in certain games it’ll instead make the player run

To give an example:
I want that when I hold the “ui_right” action, the player will walk to the right.

input = →(Hold) 

And when I pressed the action then quickly release then press and hold the “ui_right” action again, the player will run to the right.

input = → , →(Hold)

So how do I detect when such sequence happens?

// add TouchScreenButton
// add Tİmer
// and don’t forget = Project > Project Settings > General > Input Devices > Pointing set Emulate Touch From Mouse to On

var walk = true

func _on_TouchScreenButton_pressed():
  if walk == true:
     print("walking")
  else:
     print("run run")
  pass

func _on_TouchScreenButton_released():
  $Timer.start(0.5)
  walk = false
  pass

func _on_Timer_timeout():
  walk = true
  $Timer.stop()
  pass

ramazan | 2022-03-23 15:10

thanks but this does not work for all cases. I’m using input mapping so that this game will work on windows pc, android, and linux and can use an external controller so when setting up using the key mapping menu. There is no equivalent

func _on_action_pressed(input):

or

func _on_action_released(input):

as far as I know. There is only a method called

Input.is_action_pressed("ui_right")

But I dont know any ways to intercept it. So using this method will make the script really long.

Xian | 2022-03-23 23:09

:bust_in_silhouette: Reply From: w411-3

Here’s one example: https://godotforums.org/discussion/20957/double-tap-press-to-dash-help

The above version doesn’t use a process function either, but I would probably use it because it’s intuitive to me. Maybe have a new variable like last_press_timer and set it to the timeout you want (the window where you can double press) and do last_press_timer -= delta in physics_process or something, then the next time the button is pressed check if that timer is <= 0. If it isn’t, you should be good to do the double press behavior.

Lots of ways to do this one

thanks this is exactly what I need.

Xian | 2022-03-23 23:18