← Pixel Dreamer Studio

Poison, burn and bleed that keep ticking when applied again

Extend the time left on a new application, keep the tick timer running, and stack the damage if you want to.

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.

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

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