> ## Documentation Index
> Fetch the complete documentation index at: https://docs.eucaengine.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Templates & prefabs

> Define a reusable entity template once and spawn many copies from it — and learn why the bare example server returns ok:true while quietly storing nothing, so you know exactly which host you need.

A **template** is a saved `SpawnRequest` — a name plus the same fields you'd pass to `/spawn` (mesh,
color, health, team, collider, physics body, …) — that `/template/spawn` replays to create entities.
A **prefab** is the static cousin: a blueprint registered in the scene layer's `PrefabRegistry`,
instantiated by name. Both live behind a **registry resource on the world**. The mental model that
matters most here: a bare headless host does *not* register them — so this page shows you both
the working loop *and* the exact silent no-op you get without them. Every command is real and runnable
against a [local server](/quickstart); the outputs below are captured from one.

## Define a template and spawn from it

**1. Create a template.** Post a `name` plus any spawn fields. The handler runs and reports success:

```bash theme={null}
curl -s -X POST http://localhost:3917/template/create -H 'Content-Type: application/json' \
  -d '{"name":"red-cube","mesh":"cube","color":"red","health":100,"team":1}'
# {"ok":true,"template":"red-cube"}
```

**2. List the templates — and watch the trap spring.** On a bare headless host the list is *empty*,
even though step 1 returned `ok:true`:

```bash theme={null}
curl -s http://localhost:3917/template/list
# {"count":0,"templates":[]}
```

The template was never stored. `/template/create` only inserts into the registry **if one exists**
(`if let Some(registry) = w.resource_mut::<TemplateRegistry>()`), and a bare headless host never
registers it. The `ok:true` means "the handler ran," not "the template landed."

**3. Spawn from it.** With nothing stored, the spawn reports the template missing — and `entity_count`
on `GET /` does not move:

```bash theme={null}
curl -s -X POST http://localhost:3917/template/spawn -H 'Content-Type: application/json' \
  -d '{"name":"red-cube","position":[5,0,3]}'
# {"ok":false,"error":"Template 'red-cube' not found"}
```

On a host that **registers a `TemplateRegistry`** (the editor and the full game hosts do), the same
three commands close the loop: `template/list` returns `{"count":1,"templates":["red-cube"]}`, and
each `template/spawn` returns a fresh `{"ok":true,"entity_id":N,"template":"red-cube"}` — one new
entity per call from the stored blueprint, with the optional `position` overriding placement.

## Prefabs work the same way

Prefabs read from `euca_scene::PrefabRegistry`. On a bare headless host it's absent, so listing is empty
and spawning fails — but, unlike templates, the call shape is honest about it:

```bash theme={null}
curl -s http://localhost:3917/prefab/list
# {"ok":true,"count":0,"prefabs":[]}

curl -s -X POST http://localhost:3917/prefab/spawn -H 'Content-Type: application/json' \
  -d '{"name":"tower"}'
# {"ok":false,"error":"Prefab 'tower' not found in registry"}
```

A host that populates the `PrefabRegistry` makes `/prefab/list` enumerate its names and `/prefab/spawn`
return `{"ok":true,"entity_id":9,"entity_generation":0,"name":"tower"}`.

## Behavior and gotchas

The things you only find out by running it:

* **`ok:true` from `/template/create` does not mean stored.** Without a `TemplateRegistry` resource the
  insert is silently dropped and the handler still reports success. Trust `/template/list`, not the
  create response — an empty list after a create means no registry is installed.
* **Templates and prefabs differ in failure honesty.** `template/create` returns `ok:true` even when it
  no-ops; `prefab/spawn` returns `ok:false` with a clear "not found in registry". Same root cause
  (missing registry), opposite-looking responses.
* **The create body must parse as a `SpawnRequest`.** A missing `name` returns
  `{"ok":false,"error":"Template name required"}`; a body that fails to deserialize returns
  `{"ok":false,"error":"Invalid template: …"}`. The whole body (minus `name`) is stored and replayed.
* **`/template/spawn` takes a `position` override.** The stored blueprint's position is replaced by the
  `[x, y, z]` you pass, so one template seeds many entities at different spots.
* **No `/step` needed.** Templates and prefabs build entities synchronously on the call; the new entity
  shows up in `entity_count` immediately, with no tick in between.

## Endpoints

| Method | Path               | Description                                                                   |
| ------ | ------------------ | ----------------------------------------------------------------------------- |
| `POST` | `/template/create` | Store a named `SpawnRequest` (needs a `TemplateRegistry` resource to persist) |
| `POST` | `/template/spawn`  | Replay a named template into a new entity, with optional `position` override  |
| `GET`  | `/template/list`   | Names of all stored templates with a count                                    |
| `POST` | `/prefab/spawn`    | Instantiate a name from the scene `PrefabRegistry`                            |
| `GET`  | `/prefab/list`     | Names of all registered prefabs with a count                                  |

For the spawn fields a template stores, see the [quickstart](/quickstart); for the rule-driven spawning
these compose with, see [rules](/concepts/rules-as-data).

## Status

* **Shipped** — the template store/replay logic, the `SpawnRequest` field set, and prefab
  instantiation from the scene registry. All headless.
* **Host-dependent** — the registries themselves. A bare headless host registers **neither**
  `TemplateRegistry` nor `PrefabRegistry`, so on it `/template/spawn` and `/prefab/spawn` report "not
  found" and `/template/create` is a silent no-op. A host that registers the registry (the editor,
  full game hosts) makes the create→list→spawn loop work end to end.

<Card title="Template & prefab endpoints" icon="cubes" href="/api-reference/introduction" horizontal>
  Every template and prefab endpoint, with request and response schemas.
</Card>
