Is possible to manage double click of mouse?

Godot Version

4.2.1

Question

Hi guys.
I am animating my player’s sprite in _process(delta):

When I press the left mouse it starts the attack animation and continues in loop for as long as I hold the mouse down.
So far so good.
The problem is that if I press in rapid succession the attack animation starts over again, it makes sense but it is not what I want.

I would like if it was possible to interpret multiple touches of the mouse as being held down.

So if I press the mouse multiple times or hold down, the attack animation continues in a loop without resetting.

When you click the mouse, 2 events are triggered: mouse button down and mouse button released.
I was running into the problem of my mouse click code running twice because it was running on both the down and the release.

To do what you’re looking for, check for down and release and execute where appropriate.
If you want to activate on the down and keep executing while it’s held, you can check for that and then end the action on the release.

Remember, _process(delta) execute repeatedly with time involved. My game is turn-based. I only execute when the player clicks the mouse, so that’s either _input() or a custom signal.

InputEventMouseButton has double_click that you can check

Maybe I wasn’t comprehensive enough (I wrote the post at night).

So to give the example if I hold down A the character moves left with “Run Left” animation (animation involves movement of legs and arms).

If I hold down A and Left Mouse the “Attack Run Left” animation starts.

The problem is that if I press the mouse several times while running the animation starts over at frame 0 and the legs seem to be stuck.

If use are using animation player for animation…

func _process():
    if Input.is_action_pressed("LMB"):
        if anim_player.is_playing():
            if anim_player.current_animation != "attack":
               anim_player.play("attack")
        else:
              anim_player.play("attack")

I cant say it will work or not.
It would be nice if you share code

Okay I found a solution using a timer.

I set a timer called “CanAttack” of 0.4 seconds because my animation takes 4 frames per second to complete.

func _process(delta):

        # Either by holding down the mouse or by pressing repeatedly, the timer starts with a countdown of 0.4 seconds.
        if Input.is_action_just_pressed("attack") or Input.is_action_pressed("attack"):
		$CanAttack.start()
	
       # If the player is not stationary and the timer is greater than 0, the run and attack animation starts.
	if direction != Vector2.ZERO and $CanAttack.time_left > 0:
		#play animation attack and run
	else:
		#play animation run no attack

I hope I have been clear and can be of help to someone.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.