Variable PrefabsConst

Prefabs: {
    has(key: string): boolean;
    list(): string[];
    register(key: string, fn: PrefabProviderCallback): void;
    unregister(key: string): void;
} = ...

Prefab registry for networked instantiation.

When a remote syncInstantiate event is received, the engine looks up the prefab by its guid using this registry. Both GLB-loaded objects and runtime-created objects can be registered here.

Note: syncInstantiate auto-registers prefabs if no provider exists for their guid, so manual registration is only needed for custom resolution logic.

Type declaration

  • has:function
    • Check if a prefab provider is registered for the given guid.

      Parameters

      • key: string

        The guid to check

      Returns boolean

  • list:function
    • Returns a list of all registered prefab provider keys (guids). Useful for debugging to see which prefabs are available for networked instantiation.

      Returns string[]

  • register:function
    • Register a prefab provider that resolves objects by guid for networked instantiation. When a remote syncInstantiate event is received, the engine uses this to find the prefab to clone on the receiving client.

      Parameters

      • key: string

        The guid to register the provider for

      • fn: PrefabProviderCallback

        Callback that returns the prefab Object3D for the given guid

      Returns void

  • unregister:function
    • Unregister a previously registered prefab provider.

      Parameters

      • key: string

        The guid to unregister

      Returns void

import { Prefabs, ObjectUtils } from "@needle-tools/engine";

const cookie = ObjectUtils.createPrimitive("Cube", { color: 0xff8c00 });
cookie.guid = "cookie-prefab";
Prefabs.register("cookie-prefab", async () => cookie);

console.log(Prefabs.list()); // ["cookie-prefab"]
Prefabs.unregister("cookie-prefab");