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(runtime): a restarted scene draws its images again instead of white

yhh2d44fa1fe0b7
diff --git a/desktop b/desktop
index a5a37fd72..5ca93216d 160000
--- a/desktop
+++ b/desktop
@@ -1 +1 @@
-Subproject commit a5a37fd7218f75f4d5442a3338c642b63e5102d6
+Subproject commit 5ca93216d4e978f6918896466c8824af0541ebc0
diff --git a/sdk/src/runtime/runtimeLoader.ts b/sdk/src/runtime/runtimeLoader.ts
index 882db1102..543bf2db7 100644
--- a/sdk/src/runtime/runtimeLoader.ts
+++ b/sdk/src/runtime/runtimeLoader.ts
@@ -367,7 +367,11 @@ export async function prepareRuntimeScene(
// spawns prefab instances exactly as Play does (play == ship), instead of
// silently dropping them.
const runtimeAssets = ensureRuntimeAssets(app, module, source);
- let sceneData = options.sceneData;
+ // A private copy: resolution below writes handles over the asset refs, and the
+ // document given here is the one a registered scene loads from every time. A
+ // reload of the mutated one found no ref to acquire and spawned the handles the
+ // outgoing scene had just released, so a restarted scene drew its images white.
+ let sceneData = JSON.parse(JSON.stringify(options.sceneData)) as SceneData;
if (sceneHasPrefabEntries(sceneData)) {
sceneData = await timed('prefab', () => expandScenePrefabs(sceneData, async (ref) => {
try {
diff --git a/sdk/tests/runtime-assets-persistence.test.ts b/sdk/tests/runtime-assets-persistence.test.ts
index 8f1202fb7..766418bae 100644
--- a/sdk/tests/runtime-assets-persistence.test.ts
+++ b/sdk/tests/runtime-assets-persistence.test.ts
@@ -93,6 +93,24 @@ describe('per-App runtime Assets persistence', () => {
expect(assets.getManifest()).not.toBeNull();
});
+ it('leaves the scene document it loads from as authored, so a restart acquires its assets again', async () => {
+ // The document a registered scene is loaded from every time. Resolution wrote
+ // handles over its refs, so a restart found nothing to acquire and spawned the
+ // handles the outgoing scene had just released: its images drew white.
+ const app = makeFakeApp();
+ const scene = makeScene('menu');
+ scene.entities.push({
+ id: 1, name: 'backdrop', parent: null, children: [], visible: true,
+ components: [{ type: 'UIVisual', data: { visualType: 2, texture: 'assets/bg_menu.png' } }],
+ } as SceneData['entities'][number]);
+ const authored = JSON.parse(JSON.stringify(scene));
+
+ await loadScene(app, scene);
+ expect(scene).toEqual(authored);
+ await loadScene(app, scene);
+ expect(scene).toEqual(authored);
+ });
+
it('accumulates texture import settings across scenes instead of replacing them', async () => {
const app = makeFakeApp();
await loadScene(app, makeScene('a', {