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.

refactor(editor): Reconciler projects tilemap tilesetAssets (root-fix)

esengine07161a45b6df
diff --git a/desktop/src/engine/Reconciler.ts b/desktop/src/engine/Reconciler.ts
index 85d6d0c23..a220a6d18 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 } from 'esengine';
+import { Name, Parent, getComponent, resetWorldTo, TilemapLiveSync } from 'esengine';
import type { SceneData } from 'esengine';
import type { EntityId } from '@/types';
import { EngineHost } from './EngineHost';
@@ -194,6 +194,16 @@ export class ReconcilerImpl {
const cr = this.model.runtimeFor(childId);
if (cr != null) world.insert(cr, Parent, { entity: rt } as never);
}
+ // 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
+ // is what makes create AND redo/reload restore a map's tileset with no create-time
+ // special-casing (the map is a single, plain create step).
+ const layer = entity.components.find((c) => c.type === 'TilemapLayer');
+ if (layer) {
+ const refs = (layer.data as Record<string, unknown>).tilesetAssets;
+ const list = Array.isArray(refs) ? refs.filter((r): r is string => typeof r === 'string' && r !== '') : [];
+ if (list.length > 0) TilemapLiveSync.setLayerTilesets(rt, list);
+ }
}
private despawnEntity(sourceId: number): void {
diff --git a/desktop/src/engine/entitySources.ts b/desktop/src/engine/entitySources.ts
index a60059c7d..7f623e58f 100644
--- a/desktop/src/engine/entitySources.ts
+++ b/desktop/src/engine/entitySources.ts
@@ -76,11 +76,18 @@ export function spritePrefab(name: string, textureRef: string, size: { x: number
/**
* A Transform + TilemapLayer prefab whose `cellSize` (a vec2 → `{x, y}` in the model)
- * is seeded from the tileset's tile size. The `.estileset` link is out-of-band and
- * live-pushed separately (see createTilemap's afterCreate), not baked here.
+ * is seeded from the tileset's tile size. `tilesetRef` bakes the out-of-band
+ * `.estileset` link into the prefab, so it rides the single create step into the
+ * model; the Reconciler live-pushes it to the plugin on spawn (create AND
+ * redo/reload restore it), which is why creation needs no setLayerTilesets step.
*/
-export function tilemapPrefab(name: string, cellSize: { x: number; y: number }): PrefabData {
- return preset(name, [['Transform', {}], ['TilemapLayer', { cellSize: { x: cellSize.x, y: cellSize.y } }]]);
+export function tilemapPrefab(name: string, cellSize: { x: number; y: number }, tilesetRef?: string): PrefabData {
+ const layer: Record<string, unknown> = { cellSize: { x: cellSize.x, y: cellSize.y } };
+ if (tilesetRef) {
+ layer.tilesetAssets = [tilesetRef];
+ layer.tilesetAsset = tilesetRef; // back-compat singular first tileset
+ }
+ return preset(name, [['Transform', {}], ['TilemapLayer', layer]]);
}
/** A static preset source — its prefab is built once from the registered component defaults. */
diff --git a/desktop/src/tilemap/createTilemap.ts b/desktop/src/tilemap/createTilemap.ts
index 4af7327a7..ca26acf7f 100644
--- a/desktop/src/tilemap/createTilemap.ts
+++ b/desktop/src/tilemap/createTilemap.ts
@@ -11,7 +11,6 @@
*/
import { parseTileset } from 'esengine';
import { Grid3x3 } from 'lucide-react';
-import { SceneCommands } from '@/engine/SceneCommands';
import { createFromSource, tilemapPrefab, type EntitySource } from '@/engine/entitySources';
import { useSelection } from '@/store/selectionStore';
import { useTilemapPaint } from '@/store/tilemapPaintStore';
@@ -30,13 +29,10 @@ function tilesetSource(tilesetPath: string, tilesetRef: string, tileWidth: numbe
label: 'Tilemap',
category: '2D',
icon: Grid3x3,
- build: () => tilemapPrefab('Tilemap', { x: tileWidth, y: tileHeight }),
+ // tilesetRef bakes the link into the prefab; the Reconciler live-pushes it on
+ // spawn/redo, so the map is a single undoable create with no wiring step.
+ build: () => tilemapPrefab('Tilemap', { x: tileWidth, y: tileHeight }, tilesetRef),
afterCreate: (_ctx, rootId) => {
- // The .estileset link is out-of-band (carried in the model like the chunks blob).
- // setLayerTilesets writes it AND live-pushes to the plugin so the fresh layer
- // resolves its render table and is paintable immediately. It records its own
- // 'Set Tilesets' undo step, so redo re-pushes (no reload needed).
- SceneCommands.setLayerTilesets(rootId, [tilesetRef]);
useSelection.getState().select(rootId);
useTilemapPaint.getState().setTileset(tilesetPath);
useTilemapPaint.getState().setTool('brush');
diff --git a/desktop/tests/entity-sources.test.ts b/desktop/tests/entity-sources.test.ts
index 47abf79cf..1ac8c52dc 100644
--- a/desktop/tests/entity-sources.test.ts
+++ b/desktop/tests/entity-sources.test.ts
@@ -56,8 +56,16 @@ describe('entitySources registry (Create-entity E2)', () => {
const types = root.components.map((c) => c.type);
expect(types).toContain('Transform');
expect(types).toContain('TilemapLayer');
- const layer = root.components.find((c) => c.type === 'TilemapLayer')!.data as { cellSize: { x: number; y: number } };
+ const layer = root.components.find((c) => c.type === 'TilemapLayer')!.data as Record<string, unknown>;
expect(layer.cellSize).toMatchObject({ x: 32, y: 16 });
+ expect(layer.tilesetAssets).toBeUndefined(); // no ref → no out-of-band link baked
+
+ // With a tilesetRef the out-of-band link is baked into the prefab (the Reconciler
+ // live-pushes it on spawn, so no create-time setLayerTilesets step).
+ const withRef = tilemapPrefab('Tilemap', { x: 16, y: 16 }, '@uuid:ts-1').entities[0]
+ .components.find((c) => c.type === 'TilemapLayer')!.data as Record<string, unknown>;
+ expect(withRef.tilesetAssets).toEqual(['@uuid:ts-1']);
+ expect(withRef.tilesetAsset).toBe('@uuid:ts-1');
});
it('createFromSource returns null when build throws (aborted source, e.g. a failed prefab load)', async () => {