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(editor): reconciler maintains parent Children so UI children get laid out
esengine72c176ee7e33
diff --git a/desktop/src/engine/Reconciler.ts b/desktop/src/engine/Reconciler.tsindex 367196978..bba372456 100644--- a/desktop/src/engine/Reconciler.ts+++ b/desktop/src/engine/Reconciler.ts@@ -1,6 +1,6 @@// SPDX-License-Identifier: Apache-2.0// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team-import { Name, Parent, getComponent, resetWorldTo, TilemapLiveSync } from 'esengine';+import { Name, getComponent, resetWorldTo, TilemapLiveSync } from 'esengine';import type { SceneData } from 'esengine';import type { EntityId } from '@/types';import { EngineHost } from './EngineHost';@@ -192,16 +192,19 @@ export class ReconcilerImpl {const c = hidden ? foldHidden(comp) : comp;this.insertComponent(world, rt, c.type, c.data as Record<string, unknown>);}- // Link to a parent that is already spawned…+ // Link to a parent that is already spawned. setParent (not a raw Parent+ // insert) so the parent's Children list is maintained too — the UI layout's+ // buildDFS walks Children, so an insert-only link leaves the child out of+ // layout entirely (never laid out → stuck at the origin).if (entity.parent != null) {const pr = this.model.runtimeFor(entity.parent);- if (pr != null) world.insert(rt, Parent, { entity: pr } as never);+ if (pr != null) world.setParent(rt, pr);}// …and re-link any already-spawned children (undo-of-delete restores a// parent after its children, so the children await this re-parent).for (const childId of entity.children) {const cr = this.model.runtimeFor(childId);- if (cr != null) world.insert(cr, Parent, { entity: rt } as never);+ if (cr != null) world.setParent(cr, rt);}// Out-of-band: a TilemapLayer's `tilesetAssets` isn't an engine component field,// so insertComponent skips it. Live-push it to the tilemap plugin on spawn — this@@ -274,8 +277,11 @@ export class ReconcilerImpl {const entity = this.model.entityBySource(sourceId);if (!world || rt == null || !entity) return;const pr = entity.parent != null ? this.model.runtimeFor(entity.parent) : undefined;- if (pr != null) world.insert(rt, Parent, { entity: pr } as never);- else if (world.has(rt, Parent)) world.remove(rt, Parent);+ // setParent/removeParent maintain both sides (child's Parent + parent's+ // Children) — a raw Parent insert leaves the parent's Children stale, which+ // drops the child out of the UI layout's buildDFS walk.+ if (pr != null) world.setParent(rt, pr);+ else world.removeParent(rt);}private projectName(sourceId: number): void {diff --git a/desktop/tests/engine-commands.test.ts b/desktop/tests/engine-commands.test.tsindex c615c2eb8..38d890d18 100644--- a/desktop/tests/engine-commands.test.ts+++ b/desktop/tests/engine-commands.test.ts@@ -14,7 +14,7 @@* ids; the World is asserted via session.model.runtimeFor.*/import { describe, it, expect, beforeAll, beforeEach, afterEach, vi } from 'vitest';-import { App, Transform, Parent, Sprite, Interactable, BUILTIN_UI_PREFABS } from 'esengine';+import { App, Transform, Parent, Children, Sprite, Interactable, BUILTIN_UI_PREFABS } from 'esengine';import type { ESEngineModule } from 'esengine';import { loadWasmModule, HAS_WASM } from './helpers/loadWasm';@@ -142,6 +142,20 @@ describe.skipIf(!HAS_WASM)('SceneCommands / SceneQuery (headless World)', () =>expect(host.world.has(childRt, Parent)).toBe(true);});+ it('parenting maintains the parent Children list too (UI layout buildDFS walks it)', () => {+ // Regression: the Reconciler used a raw Parent-component insert, which set the+ // child's Parent but never added it to the parent's Children — so a UI child+ // was invisible to the layout's buildDFS (never laid out, stuck at the origin).+ const parent = S.commands.addEntity()!;+ const child = S.commands.addEntity()!;+ const parentRt = rt(parent);+ const childRt = rt(child);+ S.commands.setParent(child, parent);+ expect(host.world.has(parentRt, Children)).toBe(true);+ const kids = host.world.get(parentRt, Children) as { entities: ArrayLike<number> };+ expect(Array.from(kids.entities)).toContain(childRt);+ });+it('setParent rejects a cycle (parenting under a descendant)', () => {const a = S.commands.addEntity()!;const b = S.commands.addEntity()!;