If applying a status again restarts its timer, the way calling Timer.start() again does, an attacker faster than the tick interval keeps the enemy poisoned without a single tick. In our Godot 4.7 test a 5 s poison that deals 4 damage every 0.5 s, applied every 0.4 s for 10 seconds, did 0 damage during those 10 seconds and 40 after the attacks stopped. Keep the tick timer running and only extend the time left: 80 in the 10 seconds and 116 in total.
The code
Save it as status_dot.gd and keep one per status on each enemy: var poison = preload("res://status_dot.gd").new(). Call poison.apply(5.0) on every hit that poisons, and every frame health -= poison.step(delta). With max_stacks = 3 each new application also adds damage, up to three times.
extends RefCounted
## Poison, burn or bleed that can be applied again while it is still running.
## Applying again refreshes the time left without restarting the tick timer,
## so an effect applied every 0.4 s still ticks every 0.5 s.
## Call step(delta) every frame and subtract what it returns from health.
var every := 0.5 # seconds between ticks
var damage := 4.0 # per tick, per stack
var max_stacks := 1 # 3 = applying again also stacks the damage, up to 3 times
var remaining := 0.0
var stacks := 0
var _t := 0.0
func apply(duration: float) -> void:
if remaining <= 0.0:
_t = 0.0 # only a new effect starts its tick timer from zero
remaining = maxf(remaining, duration)
stacks = mini(stacks + 1, max_stacks)
func step(delta: float) -> float:
if remaining <= 0.0:
return 0.0
var dt := minf(delta, remaining)
remaining -= dt
_t += dt
var out := 0.0
while _t >= every - 0.0001: # the small margin stops float sums from losing the last tick
_t -= every
out += damage * stacks
if remaining <= 0.0:
remaining = 0.0
stacks = 0
return out
What we measured
A 5 s poison, 4 damage every 0.5 s, applied at the times below during the first 10 seconds and then left to run out, at 60 and 144 fps. Each line gives the damage during those 10 seconds, then the total. Two runs and the two frame rates gave the same numbers unless a line says otherwise.
- Applied once: 40 from every version.
- Applied every 0.4 s. Restarting on each application: 0, then 40. Ignoring new applications while it runs: 76, then 80. The script: 80, then 116.
- Applied every second. Restarting: 80, then 112. Ignoring: 72 at 60 fps and 80 at 144 fps, then 80. The script: 80, then 112.
- Applied every 6 s, after the first one had run out: 72, then 80, from all three.
- The script with
max_stacks = 3, applied every second: ticks of 4, 4, 8, 8, then 12 from 2.5 s on, 216 in the 10 seconds and 312 in total.
Why it happens
Restarting sets the tick timer back to zero, so a tick only happens when half a second passes without a new application. At one every 0.4 s that never happens, and the poison only does damage once the attacks stop. At one a second it happens to work, which is why this is easy to miss in testing. Ignoring new applications avoids the restart, but the hits that land while it runs do not extend it, so there is a gap each time it runs out before the next hit starts a new one. Extending the time left and keeping the tick timer going ticks every 0.5 s for as long as the attacks keep coming, then for 5 s more.
What to change
everyanddamage: the tick.max_stacks: 1 refreshes only, higher numbers also stack the damage.apply()keeps whichever is longer, the time left or the new duration, so a short poison never cuts a long one.- Ticks that come out one short: damage over time timer. Standing in a fire or poison area: a damage zone. Slows that stack: slow effects.
What the enemy wears while it lasts: Status Effect VFX for Godot 4 has 51 presets, including Burn, Poison in green and purple, Bleed, Shock and Freeze, each with apply(), remove() and a Duration in seconds. Its apply() starts the Duration count again, so calling it on every application with the same Duration keeps the effect on screen for as long as the script keeps ticking.
Tested in Godot 4.7 with the Compatibility renderer.
Ready-made: Status Effect VFX for Godot 4 has 51 presets, including Burn, two Poison colors, Bleed, Shock, Freeze and Weapon Auras, each with apply(), remove() and a Duration.
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
- 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
- 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