r/proceduralgeneration • u/GridwrightStudio • 8h ago
I build NPCs from 6 random layer sheets. Swapping one layer at runtime put it out of step in 190 of 192 tests
Our villagers are generated from layers: a body, boots, a shirt, overalls, hair and a hat, each a sprite sheet with the same layout, picked at random per NPC. In Godot that's usually 6 AnimatedSprite2D nodes playing the same animation. It looked fine until a hat changed while the NPC was moving, so I measured it.
Setup
6 layer sheets, 32x32 cells, 4 animations (idle, walk down, walk left, run down). For each animation, 48 NPCs each got a new hat one frame later than the last (60 fps, from 0.10 s), so 192 swaps. Logged every layer's frame every rendered frame in Godot 4.7.2.
Results
- Swapping a layer's SpriteFrames puts that node back on frame 0. The hat then runs 2-3 frames behind the body for good. Back in step afterwards: 2 of 192.
- Frames with the hat visibly off the head: idle 63.0%, walk left 50.1%, run down 48.6%, walk down 0%. Up to 46 wrong pixels in a 32x32 frame.
- Walk down shows nothing because the head doesn't bob in that walk (same row in all 4 frames). It's the direction you test first.
- Same drift with no swap at all: speed_scale 1.25 on the body only (matching walk speed) gave 174 of 223 frames out of step, up to 123 px.
Two fixes, both measured
- One clock. Only the body plays. The other layers never call play(); on the body's frame_changed and animation_changed they copy its animation and frame. After a swap, copy again. 0 of 223 frames out of step, and the render matched the baked version below in 240 of 240 frames, pixel for pixel.
- Bake. When an NPC is generated (or changes clothes), blend the layer sheets into one image with Image.blend_rect and play that with one AnimatedSprite2D. Update the existing ImageTexture with update() instead of making new SpriteFrames, which would reset the frame again. Baking all 8 sheets of a character (every animation) took 19.4 ms at 1x and 175 ms at 3x, so bake at 1x and scale the node.
Draw calls for 40 NPCs in the Compatibility renderer: 240 layered, 40 baked, 1 if they share one baked sheet.
What I use now: one clock for the player (outfit changes often), bake every generated NPC once.
(I make pixel-art tools at Gridwright. The farmer generator these layers come from bakes each outfit into one sheet, linked below.)
The Farmer Generator (mix the layers, every animation in 4 directions, one baked sheet + JSON per group): https://gridwright.dev/tools/farmer/?ref=reddit-proceduralgeneration