arcade2d
Class

PrefabRegistry

world/prefab-registry.ts:37

A name-keyed lookup table of Prefabs. The registry exists for two reasons:

  1. Uniqueness. Plain Prefab instances do not enforce name uniqueness on their own — two new Prefab({ name: 'enemy' }) calls produce two independent prefabs. Registering them through a shared PrefabRegistry surfaces the collision immediately rather than letting both quietly fight over the enemy@N id space.

  2. Spawn-by-name. When the engine eventually deserialises a world from persisted state, the saved data refers to prefabs by their name string, 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

Accessors

readonly #
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.

readonly #
prefabs: readonly Prefab[]

A snapshot of every prefab currently registered. The array is a copy — mutating it does not affect the registry, though the prefabs themselves are returned by reference.

readonly #
size: number

The number of prefabs currently registered.

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

  • name string

Returns

Prefab
#
getNullable(name: string): null | Prefab

Looks up a prefab by name. Returns null if no prefab is registered under that name, instead of throwing.

Parameters

  • name string

Returns

null | Prefab
#
has(name: string): boolean

Whether a prefab with the given name is registered.

Parameters

  • name string

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

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

  • name string

Returns

boolean
ESC