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(sdk): scene serialization detaches from live world storage
esenginea6b0367fb072
diff --git a/sdk/src/scene.ts b/sdk/src/scene.tsindex 41ec13737..bf6045ac9 100644--- a/sdk/src/scene.ts+++ b/sdk/src/scene.ts@@ -8,6 +8,7 @@import { World } from './world';import { Entity, INVALID_ENTITY } from './types';import { getComponent, Name, Camera, RuntimeOnly } from './component';+import { deepClone } from './deepClone';import { discoverSceneAssets } from './asset/discoverAssets';import { requireResourceManager } from './resourceManager';import { validateComponentData, formatValidationErrors, assetFieldNames } from './validation';@@ -585,7 +586,10 @@ export function serializeEntityComponents(world: World, entity: Entity): SceneCoif (comp.transient) continue;const data = world.tryGet(entity, comp);if (data === null) continue;- const payload = data as Record<string, unknown>;+ // tryGet returns a fresh object for builtins (convertFromWasm) but the LIVE+ // stored object for script components — clone the latter so the snapshot+ // doesn't alias running-world state (and exportData below can't mutate it).+ const payload = (comp._builtin ? data : deepClone(data)) as Record<string, unknown>;// Components with out-of-band state (e.g. TilemapLayer chunks) fold// it into the record via their registered codec.sceneComponentCodecs.get(typeName)?.exportData?.(entityNum, payload);diff --git a/sdk/tests/scene.test.ts b/sdk/tests/scene.test.tsindex 0c1a39f8c..2c65921d4 100644--- a/sdk/tests/scene.test.ts+++ b/sdk/tests/scene.test.ts@@ -16,6 +16,7 @@ import {migrateSceneData,SCENE_FORMAT_VERSION,registerSceneComponentCodec,+ serializeEntityComponents,getComponentAssetFields,getComponentAssetFieldDescriptors,getComponentSpineFieldDescriptor,@@ -63,6 +64,25 @@ describe('Scene', () => {initResourceManager(module.getResourceManager());});+ describe('serialize detaches from live storage', () => {+ it('clones script-component data so the snapshot does not alias the world', () => {+ const Stats = defineComponent('SerStats', { hp: 10, tags: [] as string[] });+ const e = world.spawn();+ world.insert(e, Stats, { hp: 10, tags: ['a'] });++ const snapshot = serializeEntityComponents(world, e);+ const snap = snapshot.find((c) => c.type === 'SerStats')!.data as { hp: number; tags: string[] };++ // Mutate the LIVE component — a detached snapshot must not change.+ const live = world.get(e, Stats) as { hp: number; tags: string[] };+ live.hp = 99;+ live.tags.push('b');++ expect(snap.hp).toBe(10);+ expect(snap.tags).toEqual(['a']);+ });+ });+// =========================================================================// Component Asset Field Registry// =========================================================================