← Pixel Dreamer Studio

A crowd of NPCs that never emote or bob in step

Give each NPC a random first delay and a new random gap every time, and start looping animations at a random point.

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.

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

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