NPCs that start their emote timers in the same frame stay in step: in our Godot 4.7 test, 20 NPCs on a 4 second timer emoted 20 at once, every 4 seconds, for the whole minute. Picking a random gap after each emote breaks it up later, but the first wave is still all 20 together. The fix is a random first delay too: with the script below the most at once was 3. For idle animations, seek() each AnimationPlayer to a random point in the loop after play().
The code
Save it as idle_spread.gd and load it with const IdleSpread := preload("res://idle_spread.gd"). Give every NPC its own IdleSpread.new(3.0, 6.0) and call tick(delta) every frame. When it returns true, play the emote: play() for a one-shot, or emitting = true for a looping one that you turn off later. In _ready(), start the idle loop with IdleSpread.random_phase($AnimationPlayer, &"idle").
extends RefCounted
## Spread idle emotes and idle animations across a crowd, so the NPCs never all
## react on the same frame or bob up and down in step.
## In each NPC:
## var idle := IdleSpread.new(3.0, 6.0) # seconds between emotes, a new random gap each time
## func _ready() -> void:
## IdleSpread.random_phase($AnimationPlayer, &"idle")
## func _process(delta: float) -> void:
## if idle.tick(delta):
## $Emote.play()
var min_gap := 3.0
var max_gap := 6.0
var _left := 0.0 # seconds until the next emote
func _init(min_seconds := 3.0, max_seconds := 6.0) -> void:
min_gap = min_seconds
max_gap = maxf(max_seconds, min_seconds)
_left = randf_range(0.0, max_gap) # the first one anywhere in the first gap, not all at once
## Call every frame. True on the frame this NPC should emote.
func tick(delta: float) -> bool:
_left -= delta
if _left > 0.0:
return false
_left += randf_range(min_gap, max_gap) # += keeps the leftover, so the gaps stay right at any frame rate
return true
## Start a looping animation at a random point in its loop.
static func random_phase(player: AnimationPlayer, anim: StringName) -> void:
player.play(anim)
player.seek(randf() * player.current_animation_length, true)
What we measured
20 NPCs for 60 seconds at 30, 60 and 144 frames a second, counting how many emoted on the same frame, with the same random seed for every run. Then 20 real AnimationPlayers with a 1 second looping idle that moves the NPC up and down, read after 20 frames. Two runs gave the same results.
- One 4 second timer each, all started together: 20 at once, every 4 seconds, from the first wave to the last, at all three frame rates.
- A new random gap of 3 to 6 seconds after each emote, but the same 4 second start: the first wave was all 20 at once. After 20 seconds they had drifted apart, at most 2 or 3 on one frame.
- The script, random first delay and random gaps: at most 3 on one frame at 30 and 60 fps and 2 at 144, from the start. Each NPC still waited 3.0 to 6.0 seconds between emotes, 4.54 on average, at every frame rate.
- 20 AnimationPlayers started with
play()on the same frame: all 20 at the same point in the loop and the same height, moving as one. Withrandom_phase(): spread over 9 of the 10 tenths of the loop, at heights from almost 0 to almost the top of the bob.
Why it happens
Every NPC in a scene runs its _ready() on the same frame, so timers and animations started there all begin together. A fixed gap keeps them together forever, and even random gaps need a few rounds to pull them apart. Starting each one somewhere random inside the first gap spreads them out from the first second, and a random point in the loop does the same for animations.
What to change
min_gapandmax_gap: seconds between one NPC's emotes. A wider range looks more natural.- The same seed every time you load, for a replay or a test: call
seed()once before the NPCs are made. - Not the same emote twice in a row: a shuffle bag. The emote over the head: an icon above a 3D character's head. Emotes the player picks: an emote wheel.
Ready-made emotes: Emote Bubbles & Comic Pops VFX for Godot 4 has 33 effects that float over 3D characters' heads, like Thinking Dots, Surprise, Anger Vein and Dizzy Stars, plus comic POW! and KO! pops, in Stylized and Retro PSX looks, with a showcase of box NPCs reacting to each other.
Tested in Godot 4.7 with the Compatibility renderer.
Ready-made: Emote Bubbles & Comic Pops VFX for Godot 4 has 33 emotes and comic pops that float over 3D characters' heads, like Thinking Dots, Surprise, Anger Vein and Dizzy Stars, in Stylized and Retro PSX looks.
More how-tos for Emote Bubbles & Comic Pops VFX for Godot 4: An icon over a 3D character's head, without the ghost behind the camera · Pick the radial menu slot the mouse is pointing at
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
- Poison, burn and bleed that keep ticking when applied again
- 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 song position that moves on every frame
- Nodes your editor tool adds that stay in the scene