← Pixel Dreamer Studio

Slows and roots that overlap

Keep a list of the slows that are on and use the strongest. Never save the old speed.

Keep every active slow in a dictionary by name, multiply the move speed by the strongest one, and remove each slow when its time runs out. Saving the old speed when a slow starts and putting it back when it ends breaks as soon as two slows overlap. In our Godot 4.7 test a character with a speed of 5 was hit by two 2-second slows of 0.5, one second apart. It dropped to 1.25 while both were on, went back to full speed while the second was still on, and was stuck at 2.5 after both had ended.

The code

Add this as a child of your character and name it Slows. In your movement code, multiply the speed by $Slows.factor(), for example velocity.x = direction.x * speed * $Slows.factor(). A spore cloud, a root or an ice spell calls add_slow() with its own name, how much to slow (0.5 is half speed, 0 is rooted) and for how many seconds.

extends Node
## Slows and roots that can overlap without leaving the character slow for good.
## Add it as a child of the character and multiply your move speed by factor().
## Call add_slow(id, slow_factor, seconds) from a root, a spore cloud or an ice spell.

var _slows := {}  # id -> [slow_factor, seconds left]


func add_slow(id: String, slow_factor: float, seconds: float) -> void:
	_slows[id] = [slow_factor, seconds]  # the same id again refreshes it, it does not stack


func remove_slow(id: String) -> void:
	_slows.erase(id)


func factor() -> float:
	var f := 1.0
	for s in _slows.values():
		f = minf(f, s[0])  # the strongest slow wins: 2 slows of 0.5 give 0.5, not 0.25
	return f


func _physics_process(delta: float) -> void:
	for id in _slows.keys():
		_slows[id][1] -= delta
		if _slows[id][1] <= 0.0:
			_slows.erase(id)

What we measured

A speed of 5. A slow of 0.5 for 2 seconds at 0 s, and another from a different source at 1 s. Two runs gave the same numbers.

Why saving the old speed fails

The second slow saves a speed that is already slowed. When the first slow ends it puts back the full speed and cancels the second one early. When the second ends it puts back the slowed speed it saved, and nothing ever undoes that. Leaving the base speed alone and working out the multiplier from the slows that are on avoids both problems.

What to change

Effects to show it: Nature & Druid VFX for Godot 4 has 31 presets, including Entangle Roots, green and toxic Spore Clouds, Earth Spikes, Thorn Rings, a Leaf Shield and healing flowers. 9 of them also come in a PSX version.

Tested in Godot 4.7 with the Compatibility renderer.

Ready-made: Nature & Druid VFX for Godot 4 has 31 ready-made druid effects, including Entangle Roots, green and toxic Spore Clouds, Thorn Rings and healing flowers, 9 of them also in a PSX version.

More how-tos for Nature & Druid VFX for Godot 4: Scatter spikes, roots or trees in a circle without overlaps

More how-tos: Hazards, traps and attacks