▶ Live demo: run this tutorial — built from src/06_births_deaths.ts. Distilled from samples/agent_lotka.ts.
AgentScript's
hatchanddiechange the population at runtime. On the GPU that means recycling buffer slots: a free-list of dead slots that a behavior pops for newborns and pushes on death. This tutorial runs true Lotka-Volterra predator/prey — predators eat prey and breed when fed, prey breed at a rate and die when caught — so the two populations boom and bust.
Enabling reproduction#
Set reproduction: true. That allocates the free-list so a behavior can call the
internal repro_spawn / repro_kill. The population is capped at population
(the carrying capacity = the free-list size):
{
population: 50_000, // carrying capacity
movement: 'velocity',
reproduction: true,
neighbors: { /* … */ },
behaviors: [{ type: 'lotka_volterra', /* vital rates … */ }],
}lotka_volterra is a built-in two-species behavior (species flagged in color.a,
predator energy in rotation). Its knobs are vital rates — preyBirthRate,
predBirthRate, eatGain, killProb, drain, etc. — all per-second so the
cycle is frame-rate independent. (Tuning lesson from building it: per-frame rates
overshoot into instant extinction; keep them per-second and ·dt.)
Seeding the initial population#
With reproduction, the emitter is off — you seed agents directly into the buffer
with app.seedAgents(data, count) from onReady. Each agent is 16 floats (the
Particle record): position, life (≥0 alive), velocity, max_life, color
(color.a = species), size, rotation (= energy for predators):
runAgentModel({
params: {},
model: ecosystem,
camera: { /* … */ },
onReady: (app) => {
const seed = buildSeed(14_000, 300); // 14k prey + 300 predators
app.seedAgents(seed.data, seed.count);
},
});A rebuild recreates the pass, so re-seed after it: app.rebuild(() => app.seedAgents(...)).
See buildSeed for the
record layout — it's the same 16-float stride readAgents returns in tutorial 8.
What you'll see#
Red predators chase green prey. Prey reproduce and bloom; predators feast, multiply, and crash the prey; then predators starve and the prey recover — a sustained oscillation, not a flat equilibrium. Off the tuned regime spatial predator/prey is extinction-prone, which is itself the NetLogo "Wolf-Sheep" lesson.
Births in your own models.
reproduction+ a behavior is the path for hatching/sprouting generally; the GPU-siderepro_spawn/repro_killlive in the behavior WGSL. Authoring brand-new reproduction logic means adding a behavior variant (see the module guide); thestepprogram can't spawn agents directly yet.
Next#
- Tutorial 7 — Links: connecting agents into a graph.