Skip to content

A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.

fix(mcp): a prefab template in an op program makes an instance, not a copy

yhh0e83a1b5ab19
diff --git a/desktop/src/engine/sceneOps.ts b/desktop/src/engine/sceneOps.ts
index 1e6ea2282..57b0ebd71 100644
--- a/desktop/src/engine/sceneOps.ts
+++ b/desktop/src/engine/sceneOps.ts
@@ -89,13 +89,16 @@ export async function applySceneOps(
): Promise<SceneOpsResult> {
if (!Array.isArray(ops)) throw new Error('applySceneOps: `ops` must be an array');
- // Phase 1 (async, no mutation): resolve every template to its PrefabData.
+ // Phase 1 (async, no mutation): resolve every template to its PrefabData, and
+ // to the prefab ref that makes it an INSTANCE rather than a copy.
const prefabs = new Map<string, PrefabData>();
+ const links = new Map<string, string | undefined>();
for (const op of ops) {
if (op.op !== 'create' || !op.template || prefabs.has(op.template)) continue;
const source = sourceById(op.template);
if (!source?.build) throw new Error(`unknown entity template: ${op.template} (see listEntityTemplates)`);
prefabs.set(op.template, await source.build({ parent: null }));
+ links.set(op.template, source.linkPrefabRef?.({ parent: null }));
}
const refs: Record<string, EntityId> = {};
@@ -135,7 +138,16 @@ export async function applySceneOps(
specs.map((c) => (typeof c === 'string' ? { type: c } : c)),
);
const position = op.x != null && op.y != null ? { x: op.x, y: op.y } : undefined;
- const id = EditorControlSurface.create(prefab, { parent, position });
+ // A prefab template must arrive as an INSTANCE — the same thing the
+ // Create menu makes. Without this ref the subtree is created as
+ // ordinary entities: the tree looks right, the batch reports success,
+ // and the scene silently holds a COPY. A driver porting twenty panels
+ // through op programs got twenty copies and no way to notice, while
+ // the identical template through `create_entity` linked correctly.
+ const id = EditorControlSurface.create(prefab, {
+ parent, position,
+ linkPrefabRef: op.template ? links.get(op.template) : undefined,
+ });
if (id == null) throw new Error('entity creation returned no id');
created.push(id);
if (op.ref) refs[op.ref] = id;
diff --git a/desktop/tests/scene-ops.test.ts b/desktop/tests/scene-ops.test.ts
index 77e38790e..0db55f461 100644
--- a/desktop/tests/scene-ops.test.ts
+++ b/desktop/tests/scene-ops.test.ts
@@ -31,8 +31,12 @@ vi.mock('@/engine/EditorSession', () => ({
}
calls.push(['transact:commit']);
},
- create: (prefab: { name: string }, opts: { parent: number | null }) => {
+ create: (prefab: { name: string }, opts: { parent: number | null; linkPrefabRef?: string }) => {
calls.push(['create', prefab.name, opts.parent]);
+ // Recorded separately so the `create` tuple keeps its shape: a prefab link
+ // is a distinct fact about the create, not another positional argument every
+ // existing assertion has to know about.
+ if (opts.linkPrefabRef) calls.push(['link', opts.linkPrefabRef]);
return state.createReturnsNull ? null : state.nextId++;
},
setField: (entity: number, component: string, key: string, _t: string, value: unknown) => {
@@ -49,8 +53,15 @@ vi.mock('@/engine/EditorSession', () => ({
vi.mock('@/engine/entitySources', () => ({
prefabFromSpecs: (name: string, specs: { type: string }[]) => ({ name, specs }),
- sourceById: (id: string) =>
- id === 'ui-image' ? { build: async () => ({ name: 'Image', specs: [] }) } : null,
+ sourceById: (id: string) => {
+ if (id === 'ui-image') return { build: async () => ({ name: 'Image', specs: [] }) };
+ // A project prefab source: it carries the ref that makes the result an
+ // instance instead of a copy.
+ if (id === 'prefab:assets/prefabs/Bar.esprefab') {
+ return { build: async () => ({ name: 'Bar', specs: [] }), linkPrefabRef: () => '@uuid:bar-1' };
+ }
+ return null;
+ },
}));
const { applySceneOps } = await import('@/engine/sceneOps');
@@ -169,3 +180,20 @@ describe('applySceneOps', () => {
await expect(applySceneOps('nope' as never)).rejects.toThrow(/must be an array/);
});
});
+
+describe('a prefab template creates an INSTANCE, not a copy', () => {
+ it('passes the linkPrefabRef its source carries through to create', async () => {
+ await applySceneOps([
+ { op: 'create', ref: 'bar', template: 'prefab:assets/prefabs/Bar.esprefab' },
+ ]);
+ // Without the ref the subtree lands as ordinary entities: the tree looks
+ // right, the batch reports success, and the scene holds a copy that no
+ // longer tracks the prefab.
+ expect(calls).toContainEqual(['link', '@uuid:bar-1']);
+ });
+
+ it('leaves a plain template unlinked', async () => {
+ await applySceneOps([{ op: 'create', ref: 'i', template: 'ui-image' }]);
+ expect(calls.some((c) => c[0] === 'link')).toBe(false);
+ });
+});