<!-- Generated by `pnpm docs:recipes`. Do not edit by hand. -->
A prefab is a scene file (ADR-0005): the same ignifx.scene format, loaded as a SceneAsset and
stamped out with world.instantiate. Instantiation is synchronous, because a SceneAsset arrives
with every asset it references already loaded; each copy gets fresh uids and a prefab link back
to the file.
Register every component the file names before loading it, or the entity is built without them
and the load reports IGX-0307.
The prefab used here is crate.prefab.json, beside this file.
typescript
import{Script,createApp,f32}from"@ignifx/core";importtype{AssetHandle,Entity,SceneAsset,ScriptCallbacks}from"@ignifx/core";/** Bobs its entity up and down. `speed` is a serialized field the prefab file sets. */classBobberextendsScript.define({ speed: f32(1.5) })implementsScriptCallbacks{statictypeId="recipes/Bobber";#elapsed=0;update(dt: number):void{this.#elapsed+=dt;this.transform.localPosition.y=0.5+Math.sin(this.#elapsed*this.speed)*0.2;}}const canvas=document.querySelector("canvas");if(!(canvasinstanceofHTMLCanvasElement)){thrownewError("ignifx renders into a <canvas> element.");}const app=awaitcreateApp({ canvas, settings: { assets: { root: "assets" } } });app.registerComponents([Bobber]);const crate: AssetHandle<SceneAsset> =awaitapp.assets.loadAsync<SceneAsset>("prefabs/crate.prefab.json");const spawned: Entity[] =[];/** * Stamps one copy of the prefab into the active scene. * * @paramx - Where to put it along the world X axis, in metres. * @returns The instance root — one entity, even when the file has several roots. */functionspawn(x: number):Entity{const root=app.world.instantiate(crate.value,{name: `Crate ${String(spawned.length)}`,position: { x, y: 0, z: 0 }, });spawned.push(root);returnroot;}for(let index=0;index<5;index+=1){spawn(index*1.5-3);}awaitapp.start();// Instances outlive the handle: releasing only stops the template from being cached.window.addEventListener("pagehide",()=>{for(const root ofspawned){root.destroy();}crate.release();app.dispose();});