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(prefab): roll back spawns when parent attachment fails

yhhf57af8ac385b
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 4d2743dea..360a5a372 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -22,6 +22,8 @@ published separately; it ships inside the editor.
### Fixed
+- **Prefab spawns roll back when their parent disappears.** Runtime instantiation checks that its requested parent accepts the root after asynchronous loading. A rejected attachment despawns the newly created hierarchy and rejects the spawn instead of leaving an unparented instance behind; live parents and the explicit no-parent sentinel remain supported.
+
- **Scene completion errors settle the transition promise.** A throwing fade-completion callback rejects the caller's promise with its original error after clearing the old overlay, so callers can recover and later scene switches remain available. A callback that starts another fade keeps that new transition intact.
- **Input cancellation stops released scroll flings.** UI scrolling discards existing coast state when the host cancels input, so a list already gliding after release remains at its current offset when the app resumes. A new press can still start a fresh drag in the same frame.
diff --git a/sdk/src/prefab.ts b/sdk/src/prefab.ts
index cd3b250cd..a3ddc2468 100644
--- a/sdk/src/prefab.ts
+++ b/sdk/src/prefab.ts
@@ -38,6 +38,7 @@ export {
export interface InstantiatePrefabOptions {
assets?: Assets;
assetBaseUrl?: string;
+ /** The parent must still accept the root after loading; failure rolls back the instance. */
parent?: Entity;
overrides?: PrefabOverride[];
}
@@ -114,8 +115,11 @@ export async function instantiatePrefab(
});
const root = entityMap.get(rootId)!;
- if (options?.parent !== undefined) {
- world.setParent(root, options.parent);
+ if (options?.parent !== undefined && !world.trySetParent(root, options.parent)) {
+ for (const entity of entityMap.values()) {
+ if (world.valid(entity)) world.despawn(entity);
+ }
+ throw new Error(`Prefab "${normalized.name}" could not attach to parent ${options.parent}`);
}
return { root, entities: entityMap };
diff --git a/sdk/tests/prefab-parent-lifetime-wasm.test.ts b/sdk/tests/prefab-parent-lifetime-wasm.test.ts
new file mode 100644
index 000000000..189bf1066
--- /dev/null
+++ b/sdk/tests/prefab-parent-lifetime-wasm.test.ts
@@ -0,0 +1,49 @@
+// SPDX-License-Identifier: Apache-2.0
+// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
+import { beforeAll, describe, expect, it } from 'vitest';
+import { App } from '../src/app/app';
+import { Parent } from '../src/ecs/component';
+import { instantiatePrefab, type PrefabData } from '../src/prefab';
+import type { ESEngineModule } from '../src/wasm';
+import { INVALID_ENTITY } from '../src/types';
+import { HAS_WASM, loadWasmModule } from './helpers/loadWasm';
+
+const prefab: PrefabData = { version: '2', name: 'parented', rootEntityId: 'root', entities: [
+ { prefabEntityId: 'root', name: 'root', parent: null, children: ['child'], visible: true, components: [] },
+ { prefabEntityId: 'child', name: 'child', parent: 'root', children: [], visible: true, components: [] },
+] };
+
+describe.skipIf(!HAS_WASM)('async prefab parenting on real WASM', () => {
+ let engine: ESEngineModule;
+ beforeAll(async () => { engine = await loadWasmModule(); });
+ it('rolls back the spawned hierarchy when its requested parent disappears', async () => {
+ const app = App.new(); app.connectCpp(new engine.Registry(), engine, { ownedRegistry: true });
+ try {
+ const parent = app.world.spawn();
+ const pending = instantiatePrefab(app.world, prefab, { parent });
+ app.world.despawn(parent);
+ await expect(pending).rejects.toThrow(/parent/);
+ expect(app.world.getAllEntities()).toHaveLength(0);
+ } finally { app.quit(); }
+ });
+ it('attaches the hierarchy to a live parent and releases it with that parent', async () => {
+ const app = App.new(); app.connectCpp(new engine.Registry(), engine, { ownedRegistry: true });
+ try {
+ const parent = app.world.spawn();
+ const result = await instantiatePrefab(app.world, prefab, { parent });
+ expect(app.world.get(result.root, Parent).entity).toBe(parent);
+ expect(app.world.getAllEntities()).toHaveLength(3);
+ app.world.despawn(parent);
+ expect(app.world.getAllEntities()).toHaveLength(0);
+ } finally { app.quit(); }
+ });
+ it('accepts the explicit no-parent sentinel', async () => {
+ const app = App.new(); app.connectCpp(new engine.Registry(), engine, { ownedRegistry: true });
+ try {
+ const result = await instantiatePrefab(app.world, prefab, { parent: INVALID_ENTITY });
+ expect(app.world.valid(result.root)).toBe(true);
+ expect(app.world.has(result.root, Parent)).toBe(false);
+ expect(app.world.getAllEntities()).toHaveLength(2);
+ } finally { app.quit(); }
+ });
+});
diff --git a/sdk/tests/prefab.test.ts b/sdk/tests/prefab.test.ts
index 2f0f22f7a..b804eac2f 100644
--- a/sdk/tests/prefab.test.ts
+++ b/sdk/tests/prefab.test.ts
@@ -22,11 +22,13 @@ const mockLoadScene = vi.mocked(loadOwnedSceneWithAssets);
function createMockWorld(): World {
const parentMap = new Map<Entity, Entity>();
+ const setParent = vi.fn((child: Entity, parent: Entity) => {
+ parentMap.set(child, parent);
+ });
return {
spawn: vi.fn(() => 0 as Entity),
- setParent: vi.fn((child: Entity, parent: Entity) => {
- parentMap.set(child, parent);
- }),
+ setParent,
+ trySetParent: vi.fn((child: Entity, parent: Entity) => { setParent(child, parent); return true; }),
insert: vi.fn(),
get: vi.fn(),
} as unknown as World;
diff --git a/tools/releaseNotes.mjs b/tools/releaseNotes.mjs
index 420fa65d0..f39642dc8 100644
--- a/tools/releaseNotes.mjs
+++ b/tools/releaseNotes.mjs
@@ -28,6 +28,8 @@ export const NOTES_FLOOR = 'v0.65.0';
* the reader recognises anyway.
*/
export const NOTED = {
+ 'fix(prefab): roll back spawns when parent attachment fails':
+ { note: 'Prefab spawns roll back when their parent disappears.' },
'fix(scene): reject transitions when completion callbacks fail':
{ note: 'Scene completion errors settle the transition promise.' },
'feat(examples): demonstrate independent sprite playback controls':