Keep a float _carry, add per_second * delta to it every frame, move int(_carry) into the whole-number stat and subtract it from the carry. Converting each frame on its own throws the fraction away: in our Godot 4.7 test, value += int(40 * delta) regenerated 300 in 10 seconds at 30 FPS and 0 at 60, 144 and 240 FPS, where 400 was expected. The script gave 399 to 400 at all four.
The code
Put it on any node, or copy its _process into your character. Set per_second to a negative number to drain, for example while a spell is channeled. changed fires only when the number really changes, so a HUD bar does not have to redraw every frame.
extends Node
## Regenerates (or drains) a whole-number stat like HP or mana by per_second, the same at any frame rate.
## The fraction left over each frame is kept for the next one instead of being thrown away.
signal changed(value: int)
@export var value := 50
@export var max_value := 100
@export var per_second := 40.0 # negative drains, for example a channeled spell
var _carry := 0.0
func _process(delta: float) -> void:
_carry += per_second * delta
var whole := int(_carry) # int() cuts toward zero, so a drain works the same way
if whole == 0:
return
_carry -= whole
var before := value
value = clampi(value + whole, 0, max_value)
if value != before:
changed.emit(value)
What we measured
We called _process with a fixed delta for 10 seconds of game time at 30, 60, 144 and 240 FPS, at 40 per second, so the right answer is 400.
int(40 * delta)every frame: 300 at 30 FPS, 0 at 60, 144 and 240 FPS.roundi(40 * delta)every frame: 300 at 30 FPS, 600 at 60 FPS, 0 at 144 and 240 FPS.- The script: 399 at 30, 60 and 240 FPS, 400 at 144 FPS. The missing point is the last fraction, still in the carry when the 10 seconds end.
- 5 per second at 60 FPS:
int()gave 0 in 10 seconds, the script 50. - Draining 12.5 per second at 144 FPS for 4 seconds took the script from 100 to 50.
- Starting at 95 with a maximum of 100, it stopped at 100 and sent
changed5 times.
Why it happens
At 60 FPS, delta is about 0.0167, so 40 per second is 0.67 per frame. int() cuts that to 0 every frame, and roundi() turns it into 1, which is 60 per second. Either way the result depends on the frame rate, and on a fast screen the stat may not move at all. A float stat avoids the problem too, but HP and mana are usually whole numbers on screen and in save files.
What to change
per_second: points per second, negative to drain.max_value: the cap. The stat stops there.
Cooldowns have a frame and pause problem of their own: ability cooldown keeps counting while paused.
A whole spell system with mana: Spell Casting Kit for Godot 4 has cast, charge and channel spells with mana and cooldowns, aiming modes, elements and status effects.
Tested in Godot 4.7 with the Compatibility renderer.
Ready-made: Spell Casting Kit for Godot 4 is a whole spell system for Godot 4: cast, charge and channel spells with mana, cooldowns, aiming modes, elements and status effects.
More how-tos for Spell Casting Kit for Godot 4: Aim a spell at the mouse on a 3D floor · A cooldown that waits while the game is paused
More how-tos: Game systems
- Hover a 3D object inside a SubViewport
- A turned building that covers the cells it stands on
- An inventory that never loses an item and never overfills a stack
- A JSON save that loads back exactly what you saved
- Fire that spreads one cell per step, in every direction
- A pooled enemy that comes back as fresh as a new one
- An enemy that stops at the dead player and finds the respawned one
- Poison, burn and bleed that keep ticking when applied again
- XP, qi and gold that keep growing without going negative
- Count slot line wins with wilds in front
- Random directions spread evenly over a sphere or a cone
- Count a lap only when the car really went round
- Make a seed repeat: daily runs, replays and roguelike seeds
- Offline progress that counts the time the game was closed
- A chance roll without the long bad-luck streaks
- Check crafting recipes in any order, with the right counts
- Damage over time with the right number of ticks
- Spawn enemies where the camera cannot see them
- Keep a chasing horde from stacking into one enemy
- Make the fish bite after the same wait at any frame rate
- Snap buildings to the right grid cell on both sides of the origin
- Make a cooking timer fire once, on time
- Pick from a weighted loot table with the right chances
- Level up as many times as the XP covers
- Walk a patrol guard to each point without overshooting
- Remove items from an Array while checking them, without skipping any
- Scatter spikes, roots or trees in a circle without overlaps
- Count and take crafting items from every stack
- Play random sounds and events without obvious repeats
- Pick the growth image so the ripe one shows exactly when the crop can be picked
- Turn a day counter into season, day and year without a day 0
- An enemy that dies once, however many hits land
- A fair shuffle you can replay
- A ghost that drives the lap at real speed
- A vision cone that does not flicker at the edge
- Move a group of units without a pile-up
- A sprinkler that waters a round patch
- A slot reel that always lands forward on the result
- A crowd of NPCs that never emote or bob in step
- A song position that moves on every frame
- Nodes your editor tool adds that stay in the scene