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.
TypeScript 2d-game-engine3d3d-game-engineandroidcppecsgame-editorgame-enginegamedeviostypescriptwebassemblywebglwebgpuwechat-minigame
fix(ecs): despawn now tears down the whole subtree (no orphaned children)
yhh0ff7fad7e0b8
diff --git a/sdk/src/world.ts b/sdk/src/world.tsindex d38bd6f5e..34b68311e 100644--- a/sdk/src/world.ts+++ b/sdk/src/world.ts@@ -5,7 +5,7 @@* @brief ECS World with C++ Registry integration*/-import { Entity, entityGeneration, entityIndex, makeEntity } from './types';+import { Entity, entityGeneration, entityIndex, makeEntity, INVALID_ENTITY } from './types';import { AnyComponentDef, ComponentDef, ComponentData, BuiltinComponentDef, isBuiltinComponent, getAllRegisteredComponents, getUserComponents, Name } from './component';import type { CppRegistry, ESEngineModule } from './wasm';import { handleWasmError } from './wasmError';@@ -208,6 +208,32 @@ export class World {'Use Commands to defer entity destruction until after iteration completes.');}+ if (!this.valid(entity)) return; // already gone (e.g. despawned as part of a subtree)++ // Unlink from the parent (setParent-to-INVALID clears both sides), then+ // tear down the whole subtree so no children are left orphaned + rendering.+ const cppRegistry = this.builtin_.getCppRegistry();+ if (cppRegistry && cppRegistry.hasParent(entity)) {+ try { cppRegistry.setParent(entity, INVALID_ENTITY); }+ catch (e) { handleWasmError(e, `despawn(detach ${entity})`); }+ }+ this.despawnSubtree_(entity, cppRegistry);+ }++ /** Depth-first teardown of `entity` and its descendants (children before parent). */+ private despawnSubtree_(entity: Entity, cppRegistry: CppRegistry | null): void {+ if (cppRegistry && cppRegistry.hasChildren(entity)) {+ const children: Entity[] = [];+ try {+ // Snapshot then free the wasm VectorEntity (it leaks if left alive).+ const vec = cppRegistry.getChildren(entity).entities;+ for (let i = 0; i < vec.size(); i++) children.push(vec.get(i) as Entity);+ vec.delete();+ } catch (e) { handleWasmError(e, `despawn(children of ${entity})`); }+ for (const child of children) {+ if (this.valid(child)) this.despawnSubtree_(child, cppRegistry);+ }+ }notifyBridge('onEntityDespawned', entity);@@ -217,7 +243,6 @@ export class World {this.names_.remove(entity);- const cppRegistry = this.builtin_.getCppRegistry();if (cppRegistry) {try {cppRegistry.destroy(entity);diff --git a/sdk/tests/entity-hierarchy.test.ts b/sdk/tests/entity-hierarchy.test.tsindex 3eb0a5aa2..143f3fd5a 100644--- a/sdk/tests/entity-hierarchy.test.ts+++ b/sdk/tests/entity-hierarchy.test.ts@@ -110,6 +110,31 @@ describe.skipIf(!HAS_WASM)('Entity Hierarchy (WASM integration)', () => {disposeWorld(world, registry);});+ it('despawn cascades to descendants and detaches from the parent', () => {+ const { world, registry } = createWorld();++ const root = world.spawn();+ const mid = world.spawn();+ const leaf = world.spawn();+ const sibling = world.spawn();++ world.setParent(mid, root);+ world.setParent(leaf, mid);+ world.setParent(sibling, root);++ world.despawn(mid);++ expect(world.valid(mid)).toBe(false);+ expect(world.valid(leaf)).toBe(false); // cascaded+ expect(world.valid(root)).toBe(true);+ expect(world.valid(sibling)).toBe(true);++ // root no longer references the despawned mid, only sibling.+ expect(module.getChildEntities(registry, root)).toEqual([sibling]);++ disposeWorld(world, registry);+ });+it('should remove parent relationship via removeParent', () => {const { world, registry } = createWorld();