← Pixel Dreamer Studio

A hold-to-craft bar that takes the same time at any frame rate and finishes once

Add the frame's delta while the button is down, finish once per press, and let the bar drain instead of snapping to 0 when the player slips.

Add delta to the progress while the button is held, not a fixed step per frame, and finish with a flag that waits for the player to let go. In our Godot 4.7 test, a ProgressBar filled with value += 1 every frame finished a 1.5 second hold in 3.00 s at 30 fps and 0.63 s at 144 fps. Checking progress >= hold_time with no flag crafted 46, 90 and 216 items from one 3 second hold at 30, 60 and 144 fps. The script below finished in 1.50 to 1.52 s at every frame rate, once per press.

The code

Save it as hold_progress.gd and load it with const HoldProgress := preload("res://hold_progress.gd"). Make one with HoldProgress.new(1.5) for each workbench, rock or tree, call update(Input.is_action_pressed("interact"), delta) every frame, and craft when it returns true. ratio() goes from 0 to 1 for the bar, and is_holding() is true while the player is working, for a looping effect.

extends RefCounted
## Hold a button to craft, mine or chop: the same time at any frame rate, one finish per hold,
## and the bar drains back instead of snapping to 0 when the player lets go for a moment.
##   var hold := HoldProgress.new(1.5)       # seconds of holding to finish
##   func _process(delta: float) -> void:
##       if hold.update(Input.is_action_pressed("interact"), delta):
##           $CraftSuccess.play()            # true on the one frame it finishes
##       $Bar.value = hold.ratio() * 100.0
##       $WorkbenchFX.emitting = hold.is_holding()

var hold_time := 1.5   # seconds of holding to finish
var drain_time := 0.5  # seconds a full bar takes to empty after letting go, 0 = back to 0 at once
var held := 0.0        # seconds of progress right now
var _wait_release := false
var _holding := false


func _init(seconds := 1.5, drain := 0.5) -> void:
	hold_time = maxf(seconds, 0.001)
	drain_time = maxf(drain, 0.0)


## Call every frame. Returns true only on the frame the hold finishes.
func update(pressed: bool, delta: float) -> bool:
	if _wait_release:  # finished: let go before the next one starts
		_holding = false
		if not pressed:
			_wait_release = false
			held = 0.0
		return false
	_holding = pressed
	if pressed:
		held += delta
		if held >= hold_time:
			held = hold_time
			_wait_release = true
			return true
	elif drain_time <= 0.0:
		held = 0.0
	else:
		held = maxf(held - delta * hold_time / drain_time, 0.0)
	return false


func ratio() -> float:
	return held / hold_time


func is_holding() -> bool:
	return _holding

What we measured

A 1.5 second hold at 30, 60 and 144 frames a second, with the button state and delta fed in frame by frame, then one run with a real input action pressed by Input.action_press() for 3 seconds of real frames. Two runs gave the same numbers.

Why it happens

A frame is a different length of time on every screen. A fixed step per frame counts frames, so a hold made for 60 fps takes twice as long at 30 and less than half as long at 144. delta is the length of the frame in seconds, so adding it counts real time. >= stays true on every frame after the bar is full, so without a flag the craft runs again on each one. Waiting for the release before the next start makes one press one craft, and draining instead of resetting forgives a thumb that slips for a moment.

What to change

Ready-made crafting effects: Crafting & Gathering VFX for Godot 4 has a Workbench Active effect to run while emitting is on, and Craft Success, Craft Fail and Rare Item to play() at the end, plus mining, chopping, forge and potion effects, 28 presets in Stylized and Retro PSX looks.

Tested in Godot 4.7 with the Compatibility renderer.

Ready-made: Crafting & Gathering VFX for Godot 4 has Workbench Active to run while crafting and Craft Success, Craft Fail and Rare Item to play() at the end, plus mining, chopping, forge and potion effects in Stylized and Retro PSX looks.

More how-tos for Crafting & Gathering VFX for Godot 4: Count and take crafting items from every stack

More how-tos: Game feel