0 votes

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 :P

Thanks in advance!

in Engine by (212 points)
edited by

2 Answers

0 votes

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")
by (212 points)
0 votes

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")
by (3,225 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.