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(tilemap): refresh layers after tileset publication changes

yhhb875cac6f63b
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 900c9fd97..a54b69e5a 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -22,6 +22,8 @@ published separately; it ships inside the editor.
### Fixed
+- **Running tilemaps follow published tileset updates.** Tileset publication versions invalidate each app's cached render table, animations and collision model after a successful replacement, without requiring a reference edit or scene reload. Failed replacements keep the working version; unchanged frames read only a version counter and avoid rebuilding the table or resolving resource paths again.
+
- **Lazy tilemaps acquire and release tileset slots correctly.** Runtime-only tileset references use owned slot acquisitions rather than the retired generic loader interface. Failed loads retry after the asset cooldown, shared references deduplicate requests, and removing the last layer or resetting the runtime releases both completed and late acquisitions.
- **Fractional tile cell edits refresh the drawn and hit grids.** Tilemap layers compare width and height independently rather than combining them into a numeric cache key. Valid fractional dimensions no longer alias another size and leave stale geometry or coordinate conversion; unchanged frames still avoid native layer reinitialization.
diff --git a/sdk/etc/index.api.md b/sdk/etc/index.api.md
index bde7e55b7..5b429a646 100644
--- a/sdk/etc/index.api.md
+++ b/sdk/etc/index.api.md
@@ -976,6 +976,7 @@ ResourceDef<AssetsClass>
@internal handleBoundTypes: () => string[]
@internal preparePrefab: (ref: string, overrides?: readonly PrefabOverride[]) => Promise<PreparedPrefab>
@internal publishedRegistryAssets: <T>(type: string) => T[]
+@internal registryAssetVersion: (type: string) => number
@internal releaseTextureLease: (lease: AssetRefLease<number>) => void
@internal releaseTyped: (type: string, ref: string) => void
@internal releaseTypedLease: (type: string, lease: AssetRefLease<PreparedLoad<unknown>>) => void
diff --git a/sdk/etc/index.native.api.md b/sdk/etc/index.native.api.md
index 86cfa14c6..a41d6f79d 100644
--- a/sdk/etc/index.native.api.md
+++ b/sdk/etc/index.native.api.md
@@ -976,6 +976,7 @@ ResourceDef<AssetsClass>
@internal handleBoundTypes: () => string[]
@internal preparePrefab: (ref: string, overrides?: readonly PrefabOverride[]) => Promise<PreparedPrefab>
@internal publishedRegistryAssets: <T>(type: string) => T[]
+@internal registryAssetVersion: (type: string) => number
@internal releaseTextureLease: (lease: AssetRefLease<number>) => void
@internal releaseTyped: (type: string, ref: string) => void
@internal releaseTypedLease: (type: string, lease: AssetRefLease<PreparedLoad<unknown>>) => void
diff --git a/sdk/etc/index.node.api.md b/sdk/etc/index.node.api.md
index 1a4fa09c3..9a02483aa 100644
--- a/sdk/etc/index.node.api.md
+++ b/sdk/etc/index.node.api.md
@@ -971,6 +971,7 @@ ResourceDef<AssetsClass>
@internal handleBoundTypes: () => string[]
@internal preparePrefab: (ref: string, overrides?: readonly PrefabOverride[]) => Promise<PreparedPrefab>
@internal publishedRegistryAssets: <T>(type: string) => T[]
+@internal registryAssetVersion: (type: string) => number
@internal releaseTextureLease: (lease: AssetRefLease<number>) => void
@internal releaseTyped: (type: string, ref: string) => void
@internal releaseTypedLease: (type: string, lease: AssetRefLease<PreparedLoad<unknown>>) => void
diff --git a/sdk/etc/minigame.api.md b/sdk/etc/minigame.api.md
index c2f399baa..72c0a856b 100644
--- a/sdk/etc/minigame.api.md
+++ b/sdk/etc/minigame.api.md
@@ -971,6 +971,7 @@ ResourceDef<AssetsClass>
@internal handleBoundTypes: () => string[]
@internal preparePrefab: (ref: string, overrides?: readonly PrefabOverride[]) => Promise<PreparedPrefab>
@internal publishedRegistryAssets: <T>(type: string) => T[]
+@internal registryAssetVersion: (type: string) => number
@internal releaseTextureLease: (lease: AssetRefLease<number>) => void
@internal releaseTyped: (type: string, ref: string) => void
@internal releaseTypedLease: (type: string, lease: AssetRefLease<PreparedLoad<unknown>>) => void
diff --git a/sdk/etc/wechat.api.md b/sdk/etc/wechat.api.md
index ff76d0c87..e1c23849f 100644
--- a/sdk/etc/wechat.api.md
+++ b/sdk/etc/wechat.api.md
@@ -971,6 +971,7 @@ ResourceDef<AssetsClass>
@internal handleBoundTypes: () => string[]
@internal preparePrefab: (ref: string, overrides?: readonly PrefabOverride[]) => Promise<PreparedPrefab>
@internal publishedRegistryAssets: <T>(type: string) => T[]
+@internal registryAssetVersion: (type: string) => number
@internal releaseTextureLease: (lease: AssetRefLease<number>) => void
@internal releaseTyped: (type: string, ref: string) => void
@internal releaseTypedLease: (type: string, lease: AssetRefLease<PreparedLoad<unknown>>) => void
diff --git a/sdk/src/asset/Assets.ts b/sdk/src/asset/Assets.ts
index 082d640f2..26320bdb8 100644
--- a/sdk/src/asset/Assets.ts
+++ b/sdk/src/asset/Assets.ts
@@ -2692,6 +2692,11 @@ export class Assets {
?? this.registrySlots_.published(type, this.assetIdentity_(ref))) as T | undefined;
}
+ /** @internal Changes when a registry type publishes, replaces or withdraws an era. */
+ registryAssetVersion(type: string): number {
+ return this.registrySlots_.version(type);
+ }
+
/**
* What the current era of a ref-bound asset took — the dependency graph as a
* PROJECTION of acquisitions, never a table anyone maintains.
diff --git a/sdk/src/asset/registryAssets.ts b/sdk/src/asset/registryAssets.ts
index 95a09ab74..37bd26aac 100644
--- a/sdk/src/asset/registryAssets.ts
+++ b/sdk/src/asset/registryAssets.ts
@@ -84,6 +84,14 @@ export class RegistryAssetSlots {
/** By (type, name) — see the note on lookup identity above. */
private byName_ = new Map<string, Slot<unknown>>();
private nextId_ = 1;
+ private versions_ = new Map<string, number>();
+
+ /** Publication counter; failed preparations and additional holders do not change it. */
+ version(type: string): number { return this.versions_.get(type) ?? 0; }
+
+ private changed_(type: string): void {
+ this.versions_.set(type, this.version(type) + 1);
+ }
/**
* A claim on the slot `key` identifies, publishing its first era if there is
@@ -119,6 +127,7 @@ export class RegistryAssetSlots {
// it is a cache hit, and publishing twice would retire what is live.
if (!slot.era) {
slot.era = era;
+ this.changed_(type);
} else if (era !== slot.era) {
era.dependencies.releaseAll();
}
@@ -152,6 +161,7 @@ export class RegistryAssetSlots {
}
const previous = slot.era;
slot.era = era;
+ this.changed_(type);
// Its dependencies only: what the name resolves to is the slot's, and
// the era above already holds it.
previous.dependencies.releaseAll();
@@ -207,6 +217,7 @@ export class RegistryAssetSlots {
const eras: Array<Preparation | null> = [];
for (const slot of new Set(this.byName_.values())) {
eras.push(slot.era);
+ if (slot.era) this.changed_(slot.type);
slot.era = null;
slot.refs = 0;
}
@@ -249,6 +260,7 @@ export class RegistryAssetSlots {
if (--slot.refs > 0) return;
const era = slot.era;
slot.era = null;
+ if (era) this.changed_(slot.type);
this.forget_(slot);
releaseEra(era);
}
diff --git a/sdk/src/tilemap/tilemapPlugin.ts b/sdk/src/tilemap/tilemapPlugin.ts
index bf4413df4..ccdd9f81a 100644
--- a/sdk/src/tilemap/tilemapPlugin.ts
+++ b/sdk/src/tilemap/tilemapPlugin.ts
@@ -19,7 +19,7 @@ import {
spawnObjectRegion, isCollisionObjectGroup, decodeTiledGid, tiledLayerComponent,
} from './tiledLoader';
import { decodeTilemapChunks } from './chunkCodec';
-import { Assets } from '../asset/AssetPlugin';
+import { Assets, type AssetsData } from '../asset/AssetPlugin';
import { resolveAssetKey } from '../asset/resolveAssetKey';
import { LazyTilesetLoads } from './lazyTilesetLoads';
import { Time } from '../ecs/resource';
@@ -124,6 +124,8 @@ export class TilemapRuntime {
const liveResolved = this.liveResolved_;
const lazyTilesetLoads = this.lazyTilesetLoads_;
let tilesetRefsChanged = true;
+ let observedAssets: AssetsData | null | undefined;
+ let tilesetVersion = -1;
// The single writer of a layer's `.estileset` ref list: point the render table
// at `refs` and mark it for re-resolve next sync. Shared by the scene codec
@@ -225,6 +227,12 @@ export class TilemapRuntime {
// next Play regenerates from the current tiles.
const playMode = playModeOnly();
const assets = app.getResource(Assets);
+ const version = assets?.registryAssetVersion?.('tileset') ?? 0;
+ if (assets !== observedAssets || version !== tilesetVersion) {
+ liveResolved.clear();
+ observedAssets = assets;
+ tilesetVersion = version;
+ }
// Physics half-extents are metres; tile sizes are pixels. Divide by the
// scene's pixelsPerUnit (Canvas, default 100) when spawning tile colliders.
let pixelsPerUnit = 100;
diff --git a/sdk/tests/registry-asset-slots.test.ts b/sdk/tests/registry-asset-slots.test.ts
index e0d71aac5..66536efd0 100644
--- a/sdk/tests/registry-asset-slots.test.ts
+++ b/sdk/tests/registry-asset-slots.test.ts
@@ -37,6 +37,34 @@ function fakeLease(key: string, released: string[]): AssetLease {
return lease;
}
+describe('publication versions', () => {
+ it('changes only for published replacements and withdrawals of its own type', async () => {
+ const slots = new RegistryAssetSlots(), { kind } = fixture();
+ expect(slots.version('tileset')).toBe(0);
+ const first = await slots.acquire(kind, 'tileset', 'atlas', ['atlas']);
+ const second = await slots.acquire(kind, 'tileset', 'atlas', ['atlas']);
+ expect(slots.version('tileset')).toBe(1);
+ expect(slots.version('tilemap')).toBe(0);
+ first.release(); expect(slots.version('tileset')).toBe(1);
+ await slots.republish(kind, 'tileset', 'atlas');
+ expect(slots.version('tileset')).toBe(2);
+ second.release(); expect(slots.version('tileset')).toBe(3);
+ const third = await slots.acquire(kind, 'tileset', 'atlas', ['atlas']);
+ expect(slots.version('tileset')).toBe(4);
+ slots.releaseAll(); expect(slots.version('tileset')).toBe(5);
+ third.release(); expect(slots.version('tileset')).toBe(5);
+ });
+
+ it('does not change when a replacement fails', async () => {
+ const slots = new RegistryAssetSlots(), { kind } = fixture();
+ const held = await slots.acquire(kind, 'tileset', 'atlas', ['atlas']);
+ const failed = { prepare: async () => { throw Error('missing'); } };
+ await expect(slots.republish(failed, 'tileset', 'atlas')).rejects.toThrow('missing');
+ expect(slots.version('tileset')).toBe(1);
+ held.release();
+ });
+});
+
/** The same, with every preparation held open until the test lets it finish. */
function gated() {
const released: string[] = [];
diff --git a/sdk/tests/tilemap-multi-app-wasm.test.ts b/sdk/tests/tilemap-multi-app-wasm.test.ts
index 26b8a96e1..c355c8f36 100644
--- a/sdk/tests/tilemap-multi-app-wasm.test.ts
+++ b/sdk/tests/tilemap-multi-app-wasm.test.ts
@@ -92,6 +92,28 @@ describe.skipIf(!HAS_WASM)('tilemap runtimes in independent apps', () => {
assets.releaseAll();
});
+ it('refreshes collision after a tileset replacement without reassigning its refs', async () => {
+ registerTilemapSupport();
+ const { app, entity, runtime, tiles } = await scene(mainCore, 1);
+ let document = JSON.stringify({ version: '1', texture: '', tileWidth: 16, tileHeight: 16,
+ columns: 1, tileCount: 1, tiles: { '1': { collision: { type: 'box' } } } });
+ const assets = Assets.create({ backend: { fetchText: async () => document,
+ resolveUrl: (path: string) => path } as unknown as Backend, module: mainCore });
+ app.insertResource(AssetsResource, assets);
+ const held = await assets.acquireTyped('tileset', 'hot.estileset');
+ runtime.setLayerTilesets(entity, ['hot.estileset']); await app.tick(1 / 60);
+ expect(isTileSolid(entity, 0, 0)).toBe(true);
+ const setTilesets = vi.spyOn(tiles, 'setTilesets');
+ document = JSON.stringify({ version: '1', texture: '', tileWidth: 16, tileHeight: 16,
+ columns: 2, tileCount: 1, tiles: {} });
+ assets.invalidate('hot.estileset'); await new Promise(r => setTimeout(r, 0)); await app.tick(1 / 60);
+ expect(isTileSolid(entity, 0, 0)).toBe(false);
+ expect(setTilesets).toHaveBeenCalledOnce();
+ for (let i = 0; i < 20; i++) await app.tick(1 / 60);
+ expect(setTilesets).toHaveBeenCalledOnce();
+ app.world.despawn(entity); await app.tick(1 / 60); held.release(); assets.releaseAll();
+ });
+
it('quitting the older app leaves the newer map and runtime alive', async () => {
const main = await scene(mainCore, 1), preview = await scene(previewCore, 2);
expect(main.entity).toBe(preview.entity);
diff --git a/tools/releaseNotes.mjs b/tools/releaseNotes.mjs
index 6260860f7..9d9cd36cc 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(tilemap): refresh layers after tileset publication changes':
+ { note: 'Running tilemaps follow published tileset updates.' },
'fix(tilemap): own lazy tileset loads and retry failures':
{ note: 'Lazy tilemaps acquire and release tileset slots correctly.' },
'fix(tilemap): compare cell dimensions independently':