Subtract every from the timer after each tick instead of setting it to 0, check it with while so one slow frame can hold several ticks, and compare against every minus a tiny margin. In our Godot 4.7 test a 6-second poison that ticks every 0.5 seconds gave 11 ticks at 30, 60, 75, 144 and 165 FPS with the usual timer code. The script below gave 12 at all five.
The code
Add it as a child of the character and call start() when the poison or burn lands. Connect ticked to your damage function and finished to whatever removes the effect.
extends Node
## Damage (or healing) over time in fixed ticks: the same number of ticks at any frame rate.
## Call start() when the poison or burn lands. ticked fires every `every` seconds, finished at the end.
signal ticked(amount: int)
signal finished
@export var per_tick := 4
@export var every := 0.5 # seconds between ticks
@export var duration := 6.0 # 12 ticks with these values
var _t := 0.0
var _left := 0.0
func start() -> void:
_t = 0.0
_left = duration
func _process(delta: float) -> void:
if _left <= 0.0:
return
var step := minf(delta, _left)
_left -= step
_t += step
while _t >= every - 0.0001: # while, not if: a slow frame can hold more than one tick
_t -= every # keep the time past the tick, do not set it back to 0
ticked.emit(per_tick)
if _left <= 0.0:
finished.emit()
What we measured
We called _process with fixed frame times for 6 seconds at 0.5 seconds per tick, so the right answer is 12. Two runs gave the same numbers.
- Setting the timer back to 0 after each tick: 11 at 30, 60, 75, 144 and 165 FPS.
- Subtracting 0.5 inside an
if: 11 at all five. - A
whileloop without the margin: 11 at all five. - The script: 12 at all five.
- Over 60 seconds, where 120 is right: setting back to 0 gave 112 at 30 FPS, 118 at 75 FPS and 119 at 165 FPS. Subtracting inside an
ifgave 119 at all three, the script 120. - At 60 FPS with one 1.2-second frame, like a loading hitch: 9 when setting back to 0, 11 with
if, 12 with the script. - With 8 seconds of frames for the 6-second poison,
finishedfired once.
Why it happens
Setting the timer back to 0 throws away the part of the frame that went past the tick. At 75 FPS a tick is 37.5 frames, so each one waits for the 38th, and the poison falls behind. At 30 FPS, 112 ticks in 1,800 frames is one every 16 frames instead of 15: fifteen times 1/30 adds up to a hair under 0.5 in floating point. An if gives at most one tick per frame, so a long frame swallows the rest. And the frame times of the whole 6 seconds add up to just under 6.0, so without a small margin the 12th tick never comes.
What to change
every: seconds between ticks.per_tick: the damage of one tick, sent withticked.duration: how long it lasts. Callingstart()again starts it over.
A fire or poison area on the ground instead: a zone that keeps hurting while you stand in it. Applied again before it runs out: extend the time left without restarting the ticks.
The look of the burn or poison: Status Effect VFX for Godot 4 has Burn, Poison, Freeze and more, each started with apply() and faded out with remove().
Tested in Godot 4.7 with the Compatibility renderer.
Ready-made: Status Effect VFX for Godot 4 has Burn, Soulfire Burn, Poison, Freeze, Petrify and more. Each one is a child of your character: apply() starts it, remove() fades it out, and Body Height scales it to your character.
More how-tos for Status Effect VFX for Godot 4: Particles from the surface of a mesh, at runtime
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
- Make HP and mana regen the same at any frame rate
- 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