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.
- Saving the old speed: 1.25 at 1.5 s, because the two slows multiplied. 5.00 at 2.5 s, although the second slow was still on. Then 2.50 at 3.5 s, and still 2.50 at 5.5 s, with no slow on at all.
- The script: 2.50 at 1.5 s, 2.50 at 2.5 s, 5.00 at 3.5 s.
- A 1-second root (0) and a 2-second chill (0.7) together: 0.00 at 0.5 s, 3.50 at 1.5 s, 5.00 at 2.5 s.
- Walking into the same spore cloud again 0.6 s later: 2.50 at 1.2 s, 5.00 at 1.8 s. The same name restarts the timer, it does not stack.
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
factor()uses the strongest slow. If you want slows to add up, multiply them together in that loop instead of usingminf, and set a lowest value so the player is never stopped by accident.remove_slow()ends a slow early, for example when a root is broken.- Speed boosts work the same way: keep them in a second dictionary and use the largest.
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
- Turn a 2D character left without the flicker
- A line or rectangle attack that hits what the player sees
- A scare that plays once, and only for the player
- An attack that hits even when a frame is slow
- Growing one cloud or aura zone grows all of them
- A parry window that is the same length on every screen
- A pair of portals that sends you through once
- An expanding shockwave that hits each enemy once
- Explosion damage that walls block, on the frame it goes off
- A fire or poison zone that keeps hurting while you stand in it
- A spike trap that hits whoever is already standing on it
- Who is inside an AoE attack circle
- Block only attacks from the front
- A sword hitbox that hits each enemy once per swing
- Blink forward without going into or through walls
- Knockback that the walk code does not cancel
- Random spots spread evenly inside a circle
- Keep a pressure plate down while anything is still on it
- Refill the guard meter only after the hits stop
- Heal from the damage that actually landed
- Let the shield take what it can and pass the rest of the hit to health
- A saw blade that slides along its path at a steady speed
- Explosion damage that reaches big enemies
- A spinning laser that hits everything it sweeps over