Best way to handle double click for player movements

Godot Version

Godot_v4.2.2-stable

Question

I am new to Godot engine and Game dev. I have search about different ways to handle double click detection for player movements. It seems to have different approach every developer. But is there a best way to achieve this?

Double click detection for horizontal and vertical movements like doing double jump, dash animation, rolling animation and etc.

Thank you in advance.

1 Like

Try adding a counter that resets every 2-5 frames every click adds to the counter, and check right before the counter resets for 1 click (single click) or 2 clicks (double click). Then and it with whatever movement direction is happening in an if statement.

1 Like

You can do this with a timer

1 Like

Could you give a sample code for this?

  1. Add Timer
  2. Add var is_dubble_press = false
  3. Set the Timer time
  4. Check if mouse input event happened–
  5. Start the timer
  6. Check if mouse input event happened again and timer.is_stopped() is not true than do something and stop the timer
2 Likes

Sure, here is the codes:

Extends Node

var timer = null
var is_doubble_click = false

func _ready():
   timer = Timer.new()
   timer.one_shot = true
   timer.wait_time = 0.25
   add_child(timer)
   timer.name = "DoubleClickTimer"

func _physical_process(delta):
   if Input.is_action_just_pressed("key_name"):
        if timer.is_stopped():
            is_double_click = false
            timer.start()
        else:
            is_double_click = true
            print("Double Click!")
1 Like

Should it be that timer must only be activated after clicking a key?

Then if the user hit the key again before timer runs out, update variables need update like

is_double_clicked = true

Then when timer runs out, just ensure to set

is_double_clicked = false
timer.stop()

1 Like