[SOLVED] Separate key tap and key hold?

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

Hello, wonderful community.

Im here again asking for your help in trying to separate the outcomes of a key TAP (very short press) and a key HOLD (holding down the key).

What im trying to do is:

  • Start the reload when the player TAPS R
  • Check ammo when the player HOLDS R

I’ve managed to achieve both correctly, but not at the same time.

With everything i tried, i end up with either the reload starting too when i try to check the ammo, or the reload starting when i finish checking ammo. Here’s the relevant code:

#CHECK AMMO
if !Gunvars.isreloading and Input.is_action_just_pressed("reload"):
	$Ammocheck.start()
	print("ammocheck start")

if Input.is_action_just_released("reload"):
	$Ammocheck.stop()
	print("ammocheck cancel")
	
#RELOAD
if Input.is_action_just_pressed("reload"):
	if Playervars.playergun == "Pistol (pickup)":
		$Pistol.call("reload")

Basically, CHECK AMMO works if you hold R down for 0.5 seconds (The duration of “Ammocheck” timer), this cancels if you interrupt the key hold.

RELOAD, on the other hand, always works, the bottom line is that i want to be able to check my ammo without reloading too.

I’ve been trying to figure this out for some time now but after a long day im burned out :stuck_out_tongue:

Thanks in advance!

:bust_in_silhouette: Reply From: Tato64

Solved it!
By adding a second “Timer”, and checking if this timer is still running (in the reload input check) i can prevent the reload from happening if the keypress was longer than “Reloadprevent” (In this case, 0.5 seconds)

#CHECK AMMO
if !Gunvars.isreloading and Input.is_action_just_pressed("reload"):
	$Reloadprevent.start()
	$Ammocheck.start()
	print("ammocheck start")
if Input.is_action_just_released("reload"):
	$Ammocheck.stop()
	print("ammocheck cancel")

#RELOAD
if Input.is_action_just_released("reload") and $Reloadprevent.is_stopped() == false:
		if Playervars.playergun == "Pistol (pickup)":
			$Pistol.call("reload")
:bust_in_silhouette: Reply From: Magso

In the reload do another check after a small amount of time to see if the action is still pressed.

if Input.is_action_just_pressed("reload"):
    yield(get_tree().create_timer(0.2),"timeout")
    if Input.is_action_pressed("reload") && Playervars.playergun == "Pistol (pickup)":
        $Pistol.call("reload")