how to do different actions depending on if an input is held or pressed

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

so i am making a game where bullets are used to open doors/used as currency and i want to make it so when you press R you put bullets into the remaining holes in the revolver cylinder (basically reserveBullets = reserveBullets + (bulletsInCylinder - 6)) but i want to make it so when you hold R it ejects from the cylinder into your reserve to be used as keys/currency (reserveBullets = reserveBullets + bulletsInCylinder/bulletsInCylinder = 0) but i can’t figure out a way of doing it without the press action lagging.

My current method:

@onready var timer = $1secTimer 

var reserveBullets = 12
var bulletsInCylinder = 4

func _physics_process(delta):
	if Input.is_action_just_pressed("reload"):
		if Input.is_action_pressed("reload"):
			reserveBullets = reserveBullets + bulletsInCylinder
			bulletsInCylinder = 0
			reserveBullets = reserveBullets + (bulletsInCylinder - 6)
:bust_in_silhouette: Reply From: jgodfrey

Here’s a first stab that I think does what you’re describing. However, it’s way more complicated than is likely necessary and can probably be refactored / simplified with some thought.

var threshold_time = 0.2
var timer = 0
var action_started = false

func _physics_process(delta):
	if Input.is_action_just_pressed("reload"):
		action_started = true
	if Input.is_action_pressed("reload") and action_started:
		timer += delta
	if timer >= threshold_time and action_started:
		action_started = false
		timer = 0

	if Input.is_action_just_released("reload"):
		if timer < threshold_time and action_started:
		action_started = false
		timer = 0

The threshold_time controls how long the reload key needs to be held down to trigger the “hold” message. If the reload key is pressed and released in less time than threshold_time, that triggers the “press” message.

So, you can add your own logic in place of the two print statements…

Note - I just edited the above code to make a minor simplification…

jgodfrey | 2023-07-01 15:21

1 Like