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.
value += 1every frame on a bar withmax_value = 90, right for 1.5 s at 60 fps: 3.00 s at 30 fps, 1.50 s at 60 and 0.63 s at 144. Always 90 frames, so the time is whatever 90 frames take on the player's screen.- Adding
deltaand checking>= hold_timewith no flag: the craft ran on every frame after the bar filled, 46, 90 and 216 times in one 3 second hold. - Setting the progress back to 0 after each craft kept crafting every 1.5 s while the button stayed down: 2 in 3 s at 30 fps, while at 60 and 144 fps the second one fell just past the 3 s.
- The script: finished at 1.500 s at 30 fps, 1.517 at 60 and 1.507 at 144, the first frame at or past 1.5 s, and once per press, also in a 10 second hold. With a hold of 4 s, a 0.5 s release and a new hold, it finished at 1.52 s and 6.02 s, so the second press started from 0. While held after the finish, the bar stayed full and
is_holding()was false. - Letting go for 0.1 s at 1.4 s and holding again: back to 0 at once finished at 2.97 to 3.02 s. With
drain_time = 0.5the bar only dropped to 69 to 72% and it finished at 1.90 to 1.98 s. Keeping the progress, withdrain_time = INF, finished at 1.60 to 1.63 s. - Real frames with the input action held for 3 seconds: it finished once and the bar stayed at 1.00.
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
hold_time: seconds to hold. Shorter for chopping a small tree, longer for a rare ore.drain_time: 0 for a bar that resets the moment the player lets go, a bigger number to forgive more,INFto keep the progress until they come back.- A machine that keeps crafting while held: when it finishes, set the progress back to 0 yourself instead of waiting for the release.
- Shake the rock or tree a little on each hit while mining: shake an object when hit. Take the ingredients from every stack: count items across stacks. A timer that finishes once on time: a cooking timer that fires on time.
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
- A swing that settles the same at any frame rate
- Screen shake that returns to center
- 3D camera shake that lands back on center
- Hit stop and slow motion
- Confetti particles with random colors
- Choppy particles: fixed_fps and interpolate
- Play animations in 12 steps a second, at the right speed on any screen
- Keep a smooth follow camera, but jump with the player when they teleport
- Make a harvested crop pop out in an arc
- Shake a rock or tree when hit, without it drifting
- Pickups that fly to the player in the same time at any frame rate
- A cooldown that waits while the game is paused
- Keep attack presses made just before the swing ends
- A dash that covers the same distance at any frame rate