← Pixel Dreamer Studio

Damage over time with the right number of ticks

Subtract the tick length instead of setting the timer back to 0, use while instead of if, and allow a tiny margin for float sums.

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.

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

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