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.

test(ai): a code registration reaches an App that was already built

esenginee45889e181e3
diff --git a/sdk/src/ai/bt/BehaviorTreeAgent.ts b/sdk/src/ai/bt/BehaviorTreeAgent.ts
index 15c265927..1bc4d5cfc 100644
--- a/sdk/src/ai/bt/BehaviorTreeAgent.ts
+++ b/sdk/src/ai/bt/BehaviorTreeAgent.ts
@@ -39,7 +39,9 @@ export const BehaviorTreeAgent = defineComponent<BehaviorTreeAgentData>('Behavio
},
});
-/** Behavior trees keyed by registration name or `.esbt` asset path. A tree IS its definition — no compile. */
+/** Trees a game registered in CODE, process-wide — see the FSM store for why it
+ * is live rather than copied per App. An `.esbt` belongs to its realm, not
+ * here. A tree IS its definition — no compile. */
const btStore = new Map<string, BtDefinition>();
export function registerBt(key: string, def: BtDefinition): BtDefinition {
diff --git a/sdk/src/ai/fsm/StateMachineAgent.ts b/sdk/src/ai/fsm/StateMachineAgent.ts
index 2922ec99a..b997ce370 100644
--- a/sdk/src/ai/fsm/StateMachineAgent.ts
+++ b/sdk/src/ai/fsm/StateMachineAgent.ts
@@ -41,10 +41,15 @@ export const StateMachineAgent = defineComponent<StateMachineAgentData>('StateMa
},
});
-/** Compiled FSMs keyed by registration name or `.esfsm` asset path. */
+/**
+ * FSMs a game registered in CODE, process-wide. An `.esfsm` does not land here —
+ * it belongs to the realm that loaded it, which a lookup asks first. Live rather
+ * than copied per App: hot reload re-imports the bundle into a RUNNING app, so a
+ * copy taken at build would answer with the version from before the edit.
+ */
const fsmStore = new Map<string, CompiledFsm>();
-/** Register (compile) an FSM under `key`; code path and `.esfsm` loader share this store. */
+/** Register (compile) an FSM under `key` — the code half of the registry. */
export function registerFsm(key: string, def: FsmDefinition): CompiledFsm {
const compiled = compileFsm(def);
fsmStore.set(key, compiled);
diff --git a/sdk/src/animation/Animator.ts b/sdk/src/animation/Animator.ts
index 2a5129f6f..ff988c130 100644
--- a/sdk/src/animation/Animator.ts
+++ b/sdk/src/animation/Animator.ts
@@ -376,12 +376,10 @@ export const Animator: ComponentDef<AnimatorData> = defineComponent('Animator',
});
/**
- * Global controller store keyed by `.esanimator` asset path — the runtime twin of
- * the per-App {@link AnimatorControllerAPI.registerController} name registry. The
- * asset loader registers a disk controller here (the App resource isn't reachable
- * from a `LoadContext`), and {@link AnimatorControllerAPI} falls back to it when an
- * Animator's `controller` is a path rather than a code-registered name. Mirrors the
- * FSM store in {@link file://./ai/fsm/StateMachineAgent.ts}.
+ * Controllers a game registered in CODE by path, process-wide. Not where a
+ * `.esanimator` lands — an asset belongs to the realm that loaded it, which is
+ * why {@link AnimatorControllerAPI.getController} asks its own registrations,
+ * then its realm, and only then here.
*/
const animatorControllerStore = new Map<string, AnimatorControllerDef>();
diff --git a/sdk/tests/realm-registry-isolation.test.ts b/sdk/tests/realm-registry-isolation.test.ts
index 2119769c0..f9928526f 100644
--- a/sdk/tests/realm-registry-isolation.test.ts
+++ b/sdk/tests/realm-registry-isolation.test.ts
@@ -18,7 +18,13 @@ import { Catalog } from '../src/asset/Catalog';
import { Assets as AssetsResource } from '../src/asset/AssetPlugin';
import { fsmTouches } from '../src/ai/fsm/FsmPlugin';
import { btTouches } from '../src/ai/bt/BtPlugin';
-import { clearFsmStore } from '../src/ai/fsm/StateMachineAgent';
+import { StateMachineAgent, clearFsmStore, registerFsm,
+ type StateMachineAgentData } from '../src/ai/fsm/StateMachineAgent';
+import { FsmPlugin, stepStateMachines } from '../src/ai/fsm/FsmPlugin';
+import { appRegistryAsset } from '../src/asset/registryLookup';
+import type { CommandsInstance } from '../src/ecs/commands';
+import type { AnyComponentDef, ComponentData } from '../src/ecs/component';
+import type { Entity } from '../src/types';
import { clearBtStore } from '../src/ai/bt/BehaviorTreeAgent';
import { ensureBuiltinAiRegistrations } from '../src/ai/builtins';
import type { Backend } from '../src/asset/Backend';
@@ -62,6 +68,26 @@ function realm(text: (url: string) => string): { app: App; assets: Assets } {
return { app, assets };
}
+/** One entity carrying a StateMachineAgent — all `stepStateMachines` reads. */
+class AgentWorld {
+ private data = new Map<Entity, StateMachineAgentData>();
+ private next = 1;
+
+ spawn(agent: StateMachineAgentData): Entity {
+ const entity = this.next++ as Entity;
+ this.data.set(entity, agent);
+ return entity;
+ }
+ getEntitiesWithComponents(): readonly Entity[] { return [...this.data.keys()]; }
+ get<C extends AnyComponentDef>(entity: Entity, _component: C): ComponentData<C> {
+ return this.data.get(entity) as ComponentData<C>;
+ }
+ set<C extends AnyComponentDef>(entity: Entity, _component: C, value: ComponentData<C>): void {
+ this.data.set(entity, value as StateMachineAgentData);
+ }
+ has(): boolean { return true; }
+}
+
describe('two realms in one process do not share an asset', () => {
beforeEach(() => { clearFsmStore(); clearBtStore(); ensureBuiltinAiRegistrations(); });
@@ -155,6 +181,28 @@ describe('two realms in one process do not share an asset', () => {
expect(ctrl.getController('enemy.esanimator')?.initialState).toBe('run');
});
+ it('a registration made after the App was built still reaches it', () => {
+ // Hot reload re-imports the bundle into a LIVE app, so a module-scope
+ // `registerFsm` runs after build on every edit. An app holding a copy
+ // taken at build would answer with the version from before it, forever.
+ const a = realm(() => fsmWriting('Sprite'));
+ new FsmPlugin().build(a.app);
+ registerFsm('lateComer', JSON.parse(fsmWriting('Camera')) as never);
+
+ expect(fsmTouches(a.app).writes, 'the app answered from a copy taken at build')
+ .toContain('Camera');
+
+ // And the step RUNS it, not only the schedule declaring it: the realm
+ // resolver answers nothing for a name that is not an asset.
+ const world = new AgentWorld();
+ const entity = world.spawn({ fsm: 'lateComer', current: '' });
+ stepStateMachines(
+ world, {} as CommandsInstance, 1 / 60, new Map(),
+ (ref) => appRegistryAsset(a.app, 'statemachine', ref),
+ );
+ expect(world.get(entity, StateMachineAgent).current).toBe('Act');
+ });
+
it('a code registration has no realm, and every app still sees it', async () => {
// The other half of the rule: `registerFsm('patrol', …)` is not an asset.
const { registerFsm } = await import('../src/ai/fsm/StateMachineAgent');