PrefabRegistry
world/prefab-registry.ts:37A name-keyed lookup table of Prefabs. The registry exists for two reasons:
Uniqueness. Plain
Prefabinstances do not enforce name uniqueness on their own — twonew Prefab({ name: 'enemy' })calls produce two independent prefabs. Registering them through a sharedPrefabRegistrysurfaces the collision immediately rather than letting both quietly fight over theenemy@Nid space.Spawn-by-name. When the engine eventually deserialises a world from persisted state, the saved data refers to prefabs by their
namestring, not by any in-memory reference. A registry is the canonical place to resolve those strings back to the prefab objects that know how to rebuild them.
Registries are independent of World instances — a registry can be
attached to one world, shared across many, or used purely as a directory
lookup. Attach a registry to a world via
WorldOptions.prefabs to enable
World.createFromPrefabName.
Example
const prefabs = new PrefabRegistry();
prefabs.register(PlayerPrefab);
prefabs.register(EnemyPrefab);
const world = game.createWorld({ components: setup, prefabs });
world.createFromPrefabName('enemy', new Point(100, 100));Constructors
constructor(prefabs: readonly Prefab[]): PrefabRegistry Constructs a new registry, optionally seeded with an initial set of prefabs. Equivalent to constructing an empty registry and then calling PrefabRegistry.register on each entry.
Parameters
-
prefabsreadonly Prefab[]
Accessors
names: readonly string[] A snapshot of the names of every prefab currently registered. The array is a copy — mutating it does not affect the registry.
Methods
get(name: string): Prefab Looks up a prefab by name. Throws if no prefab is registered under that name; use PrefabRegistry.getNullable when absence is a valid outcome.
Parameters
-
namestring
Returns
Prefab has(name: string): boolean Whether a prefab with the given name is registered.
Parameters
-
namestring
Returns
boolean register(prefab: Prefab): void Registers a prefab under its own name. Throws if a prefab with that
name is already registered — replacement is intentionally not supported
because it almost always indicates a mistake (e.g. two modules both
declaring an enemy prefab). If you genuinely want to swap, call
PrefabRegistry.unregister first.
Parameters
-
prefabPrefab
Returns
void unregister(name: string): boolean Removes a prefab from the registry. Returns true if a prefab was
removed, false if no prefab with that name existed. The prefab object
itself is not destroyed — it remains usable for direct
World.createFromPrefab calls, just no longer reachable by name
through this registry.
Parameters
-
namestring
Returns
boolean