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(sdk): persistent entity surviving scene unload is promoted to global

esengine454143e95a79
diff --git a/sdk/src/sceneManager.ts b/sdk/src/sceneManager.ts
index 9b0b2ec30..f910750ec 100644
--- a/sdk/src/sceneManager.ts
+++ b/sdk/src/sceneManager.ts
@@ -447,7 +447,19 @@ export class SceneManagerState {
if (keepPersistent && this.app_.world.valid(entity) &&
this.app_.world.has(entity, SceneOwner)) {
const data = this.app_.world.get(entity, SceneOwner);
- if (data.persistent) continue;
+ if (data.persistent) {
+ // A persistent entity outlives its origin scene, so it is now
+ // global. Clear the owning-scene tag — otherwise every
+ // scene-gated consumer (the camera render filter, system/draw
+ // gating) still sees it owned by a scene that no longer exists
+ // and orphans it (a persistent camera would stop rendering).
+ // scene === '' is the existing "always active" rule.
+ if (data.scene !== '') {
+ data.scene = '';
+ this.app_.world.insert(entity, SceneOwner, data);
+ }
+ continue;
+ }
}
if (this.app_.world.valid(entity)) {
this.app_.world.despawn(entity);
diff --git a/sdk/tests/sceneManager.test.ts b/sdk/tests/sceneManager.test.ts
index 793edfd25..c8c82c817 100644
--- a/sdk/tests/sceneManager.test.ts
+++ b/sdk/tests/sceneManager.test.ts
@@ -270,6 +270,13 @@ describe('SceneManager', () => {
expect(app.world.despawn).not.toHaveBeenCalledWith(1);
// Entity 2 should be despawned (not persistent)
expect(app.world.despawn).toHaveBeenCalledWith(2);
+
+ // The surviving persistent entity is promoted to global (scene ''),
+ // so scene-gated consumers (e.g. the camera render filter) keep it
+ // active instead of orphaning it to the now-unloaded scene.
+ const owner1 = app._entities.get(1)!.get(SceneOwner);
+ expect(owner1.persistent).toBe(true);
+ expect(owner1.scene).toBe('');
});
it('unload() with keepPersistent=false despawns all entities', async () => {