↓
iaen.es Devlog Leer en español

Gotic · 3 min read

Sheep that follow your footsteps: breadcrumb herding in Godot 4

Three lost sheep, two wolves and a whistle. To get the flock around trees and fences without any pathfinding, the sheep step exactly where you stepped.

#animals#quests#mods#godot#procedural

Sheep that follow your footsteps: breadcrumb herding in Godot 4

Nara the shepherd is waiting at the pen in the north-west. She has lost Nube, Mora and Trufa somewhere in the meadow and hands you a whistle to bring them back. Press V (or SILBAR, "whistle", on a phone) and any sheep within 14 m starts following you. You have to lead them slowly back to the pen and in through the north or south gate. Two wolves prowl the heath trying to scare them; whistling calms the sheep and drives the wolves off for a few seconds.

When you're done, Nara gives you walking boots (+10 % speed on foot) and Brizna, a little dog who tags along from then on. This is El rebaño perdido ("the lost flock"), another standalone mod: it needs neither the village nor the broom.

The three sheep in their hay beds
Quest complete: Nube, Mora and Trufa safe in the pen

How it's built

The hard part was keeping the sheep from getting stuck against a tree or the pen fence. I could have computed paths, but there's a cheaper trick: make them step where you stepped. Each sheep keeps a breadcrumb trail, adding a point whenever you move 1.2 m from the last one (96 at most), and walks toward the oldest crumb; once it's within 0.85 m, the crumb is dropped and it heads for the next. If you went around a trunk, so does the sheep.

if trail.is_empty() or trail.back().distance_to(player.global_position) > 1.2:
	trail.append(player.global_position)
if trail.size() > 96:
	trail.pop_front()
while not trail.is_empty() and _flat_distance(trail[0]) < 0.85:
	trail.pop_front()

The sheep, the wolves and Brizna are CharacterBody3Ds with real physics: they collide with the terrain and the fences. To stop them climbing on top of each other, each sheep nudges away from any other within 1 m. They walk at 4.8 m/s, and if you get more than 24 m ahead, swim or take off on the broom, they stop and wait. Pressing R next to one tells it to wait or to follow again, so you can bring them all together or one at a time.

Wolves telegraph before lunging, like the shadows in the firefly lantern quest. A frightened sheep runs for a few seconds and then stands still, waiting for you to whistle nearby. No animal is ever lost for good: those that reach the pen stay safe in their hay bed and never get scared again, and the missing ones return to their search spots when you load the game.

Measured on PC: 541 checks with no failures or warnings, 63 of them for the flock. The whole quest is played through with the engine's physics and live wolves, including a real wolf charge, the whistle pressed with a second finger, the boots' speed, Brizna following and coming back, and half-finished and completed saves. It also checks that turning the mod off removes the animals, fences, whistle, boots and their bonus. The closest screenshot of Brizna cost 421 draw calls and 177,393 primitives.

What went wrong

Nothing broke in the game, but the flock taught me something about testing: every test game loads every mod, so the suite grows even when a mod's own tests don't change. With 16 mods, the flock's tests went from 14 to 31 s without touching a line. Since then I measure the total after every new mod.

Still unverified

It hasn't been tried on a physical phone. Nor do I know whether herding three sheep at once is fun or maddening: real players will tell, not the tests.

More from Gotic →