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): a game's own action can declare its parameters

yhh51ccbe07542c
diff --git a/sdk/etc/index.api.md b/sdk/etc/index.api.md
index d38660595..f0e37c5de 100644
--- a/sdk/etc/index.api.md
+++ b/sdk/etc/index.api.md
@@ -10572,7 +10572,7 @@ PrefabsPlugin
## registerAction — function @experimental
```
-(name: string, fn: AiAction<AiContext>): void
+(name: string, fn: AiAction<AiContext> | AiActionSpec<AiContext>): void
```
## registerAnimatorController — function @experimental
diff --git a/sdk/etc/index.native.api.md b/sdk/etc/index.native.api.md
index 21f9f3b4a..e7f09cf2e 100644
--- a/sdk/etc/index.native.api.md
+++ b/sdk/etc/index.native.api.md
@@ -10815,7 +10815,7 @@ PrefabsPlugin
## registerAction — function @experimental
```
-(name: string, fn: AiAction<AiContext>): void
+(name: string, fn: AiAction<AiContext> | AiActionSpec<AiContext>): void
```
## registerAnimatorController — function @experimental
diff --git a/sdk/etc/index.node.api.md b/sdk/etc/index.node.api.md
index 29570031e..e06a7513c 100644
--- a/sdk/etc/index.node.api.md
+++ b/sdk/etc/index.node.api.md
@@ -10577,7 +10577,7 @@ PrefabsPlugin
## registerAction — function @experimental
```
-(name: string, fn: AiAction<AiContext>): void
+(name: string, fn: AiAction<AiContext> | AiActionSpec<AiContext>): void
```
## registerAnimatorController — function @experimental
diff --git a/sdk/etc/minigame.api.md b/sdk/etc/minigame.api.md
index b82528009..8b9d131fd 100644
--- a/sdk/etc/minigame.api.md
+++ b/sdk/etc/minigame.api.md
@@ -10827,7 +10827,7 @@ PrefabsPlugin
## registerAction — function @experimental
```
-(name: string, fn: AiAction<AiContext>): void
+(name: string, fn: AiAction<AiContext> | AiActionSpec<AiContext>): void
```
## registerAnimatorController — function @experimental
diff --git a/sdk/etc/wechat.api.md b/sdk/etc/wechat.api.md
index bdea36002..db786884a 100644
--- a/sdk/etc/wechat.api.md
+++ b/sdk/etc/wechat.api.md
@@ -10599,7 +10599,7 @@ PrefabsPlugin
## registerAction — function @experimental
```
-(name: string, fn: AiAction<AiContext>): void
+(name: string, fn: AiAction<AiContext> | AiActionSpec<AiContext>): void
```
## registerAnimatorController — function @experimental
diff --git a/sdk/src/ai/fsm/AiContext.ts b/sdk/src/ai/fsm/AiContext.ts
index 67770b7b8..eb31e681a 100644
--- a/sdk/src/ai/fsm/AiContext.ts
+++ b/sdk/src/ai/fsm/AiContext.ts
@@ -16,7 +16,7 @@ import type { World } from '../../ecs/world';
import type { CommandsInstance } from '../../ecs/commands';
import type { AnyComponentDef, ComponentData } from '../../ecs/component';
import type { Blackboard } from './Blackboard';
-import { AiRegistry, type AiAction, type AiCondition } from './registry';
+import { AiRegistry, type AiAction, type AiActionSpec, type AiCondition } from './registry';
export interface AiContext {
/** The agent entity this action/condition runs for. */
@@ -44,8 +44,12 @@ export interface AiContext {
*/
export const aiRegistry = new AiRegistry<AiContext>();
-/** Register a named action referenced by FSM state hooks / BT leaves. */
-export function registerAction(name: string, fn: AiAction<AiContext>): void {
+/**
+ * Register a named action referenced by FSM state hooks / BT leaves / event
+ * rows — as a bare function, or with declared parameters, which is what turns
+ * an editor text box into typed controls.
+ */
+export function registerAction(name: string, fn: AiAction<AiContext> | AiActionSpec<AiContext>): void {
aiRegistry.registerAction(name, fn);
}
diff --git a/sdk/tests/ai-action-params.test.ts b/sdk/tests/ai-action-params.test.ts
index 2aea190f0..f0a3dd898 100644
--- a/sdk/tests/ai-action-params.test.ts
+++ b/sdk/tests/ai-action-params.test.ts
@@ -16,6 +16,7 @@ import {
type AiParams,
} from '../src/ai/fsm/registry';
import { Blackboard } from '../src/ai/fsm/Blackboard';
+import { aiRegistry, registerAction, type AiContext } from '../src/ai/fsm/AiContext';
const PAIR: AiParamDef[] = [
{ name: 'controller', type: 'enum' },
@@ -112,3 +113,29 @@ describe('a registered action sees both shapes, whichever the caller has', () =>
expect(reg.getActionParams('unknown')).toEqual([]);
});
});
+
+describe('the public registerAction', () => {
+ const bb = new Blackboard();
+ const ctx = {} as AiContext;
+
+ it('takes a declaration, so a game action reaches the same typed controls', () => {
+ let seen: number | undefined;
+ registerAction('test.award', {
+ params: [{ name: 'amount', type: 'number' }],
+ run: (_ctx, _bb, _arg, params) => { seen = params?.amount as number; },
+ });
+
+ expect(aiRegistry.getActionParams('test.award')).toEqual([{ name: 'amount', type: 'number' }]);
+ invokeAction(aiRegistry, 'test.award', ctx, bb, { arg: '25' });
+ expect(seen).toBe(25);
+ });
+
+ it('still takes a bare function', () => {
+ let seen: string | undefined;
+ registerAction('test.plain', (_ctx, _bb, arg) => { seen = arg; });
+
+ expect(aiRegistry.getActionParams('test.plain')).toEqual([]);
+ invokeAction(aiRegistry, 'test.plain', ctx, bb, { arg: 'raw' });
+ expect(seen).toBe('raw');
+ });
+});