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(scene): an entity promoted out of a sleeping scene comes out awake
esengineeaf369d9ac6d
diff --git a/sdk/src/scene/sceneManager.ts b/sdk/src/scene/sceneManager.tsindex 351fc9156..9fe18f06b 100644--- a/sdk/src/scene/sceneManager.ts+++ b/sdk/src/scene/sceneManager.ts@@ -463,7 +463,7 @@ export class SceneManagerState {* scene — a failed load cannot promote an entity into a global one.*/private rollbackFailedLoad_(name: string, instance: SceneInstance): void {- this.disposeSceneOwnedState_(instance, false);+ this.disposeSceneOwnedState_(instance, false, false);this.removeSceneSlot_(name, instance);}@@ -474,7 +474,9 @@ export class SceneManagerState {* An `unload` during a load tears the instance down, and that load's own* rollback then runs over it again — over whatever its setup added between.*/- private disposeSceneOwnedState_(instance: SceneInstance, keepPersistent: boolean): void {+ private disposeSceneOwnedState_(+ instance: SceneInstance, keepPersistent: boolean, wasSleeping: boolean,+ ): void {const world = this.app_.world;for (const entity of instance.entities) {if (keepPersistent && world.valid(entity) && world.has(entity, SceneOwner)) {@@ -490,6 +492,10 @@ export class SceneManagerState {data.scene = '';world.insert(entity, SceneOwner, data);}+ // The scene's sleep does not follow it out. Nothing will+ // wake this entity once the scene is gone, and the record of+ // what it looked like awake dies with the instance below.+ if (wasSleeping) this.restoreSleepState_(instance, entity);continue;}}@@ -542,6 +548,9 @@ export class SceneManagerState {if (!instance) return;const ctx = this.contexts_.get(name)!;+ // Read before the status becomes 'unloading': what a promoted entity+ // needs put back is what sleep() took, and only the old status says so.+ const wasSleeping = instance.status === 'sleeping';instance.status = 'unloading';if (instance.config.cleanup) {@@ -554,7 +563,7 @@ export class SceneManagerState {}}- this.disposeSceneOwnedState_(instance, options?.keepPersistent ?? true);+ this.disposeSceneOwnedState_(instance, options?.keepPersistent ?? true, wasSleeping);this.removeSceneSlot_(name, instance);}@@ -722,21 +731,25 @@ export class SceneManagerState {this.sleepingScenes_.delete(name);this.setPostProcessPassesEnabled(instance, true);+ for (const entity of instance.entities) this.restoreSleepState_(instance, entity);+ instance.savedEnabled.clear();+ }++ /** Put one entity back the way {@link sleep} found it. Shared with the+ * promotion of a persistent entity out of a sleeping scene, which is the+ * same question asked about one entity instead of all of them. */+ private restoreSleepState_(instance: SceneInstance, entity: Entity): void {const world = this.app_.world;- for (const entity of instance.entities) {- if (!world.valid(entity)) continue;- world.remove(entity, Disabled);- const entitySaved = instance.savedEnabled.get(entity);- if (!entitySaved) continue;- for (const [comp, wasEnabled] of entitySaved) {- if (world.has(entity, comp)) {- const data = world.get(entity, comp) as Record<string, unknown>;- data[comp.renderableField] = wasEnabled;- world.set(entity, comp, data as never);- }- }+ if (!world.valid(entity)) return;+ world.remove(entity, Disabled);+ const entitySaved = instance.savedEnabled.get(entity);+ if (!entitySaved) return;+ for (const [comp, wasEnabled] of entitySaved) {+ if (!world.has(entity, comp)) continue;+ const data = world.get(entity, comp) as Record<string, unknown>;+ data[comp.renderableField] = wasEnabled;+ world.set(entity, comp, data as never);}- instance.savedEnabled.clear();}private setPostProcessPassesEnabled(instance: SceneInstance, enabled: boolean): void {diff --git a/sdk/tests/scene-load-transaction.test.ts b/sdk/tests/scene-load-transaction.test.tsindex 636b82290..4bbfb0ab7 100644--- a/sdk/tests/scene-load-transaction.test.ts+++ b/sdk/tests/scene-load-transaction.test.ts@@ -22,6 +22,7 @@ import { Assets } from '../src/asset/Assets';import { AssetScope } from '../src/asset/AssetLease';import { PostProcess } from '../src/postprocess';import { getDrawCallbacks, clearDrawCallbacks } from '../src/render/customDraw';+import { Disabled, defineComponent } from '../src/ecs/component';import type { Backend } from '../src/asset/Backend';import type { Entity } from '../src/types';@@ -77,6 +78,10 @@ function harness() {const CAMERA = 7 as Entity;+/** Something sleep() switches off — the engine's renderables are C++-backed,+ * and what is under test is the protocol, not the component. */+const Drawn = defineComponent('SleepPromotionDrawn', { visible: true }, { renderableField: 'visible' });+describe('a scene load that fails leaves nothing of itself behind', () => {beforeEach(() => { clearDrawCallbacks(); });@@ -200,6 +205,32 @@ describe('a scene load that fails leaves nothing of itself behind', () => {expect(manager.getScene('level')).toBeNull();});+ it('an entity promoted out of a SLEEPING scene comes out awake', async () => {+ // sleep() keeps the record of what an entity looked like awake on the+ // instance, and the instance is gone a line after the promotion — so an+ // entity carried out asleep can never be woken by anything.+ const { app, manager } = harness();+ let spawned = 0 as Entity;++ manager.register({+ name: 'level',+ setup: (ctx) => {+ spawned = ctx.spawn();+ app.world.insert(spawned, Drawn, { visible: true });+ ctx.setPersistent(spawned, true);+ },+ });+ await manager.load('level');+ manager.sleep('level');+ expect(app.world.has(spawned, Disabled), 'sleep() disables the scene').toBe(true);++ await manager.unload('level');++ expect(app.world.valid(spawned)).toBe(true);+ expect(app.world.has(spawned, Disabled), 'promoted still asleep, with nothing left to wake it').toBe(false);+ expect(app.world.get(spawned, Drawn).visible, 'and visible again').toBe(true);+ });+it('a failed load cannot promote an entity to global ownership', async () => {// Persistence is a property of a COMMITTED scene. An entity marked// persistent inside a setup that then threw was never part of a scene