《游戏编辑器技术原理与架构》6.3 多语言脚本类型桥

编辑器运行时是 TypeScript,游戏脚本可能是 C#、Lua 或未来的 WASM 模块——两套类型系统怎么对上?本节实现 TypeBridge:维护「脚本类型 ↔ 编辑器类型」的双向映射,convert 把脚本值经映射转成编辑器对象;6 条用例本机真跑,为脚本属性暴露与多语言绑定定下转换规则。

6.3 多语言脚本类型桥

编辑器的运行时用 TypeScript;游戏的脚本可以是 C#、Lua,或者未来的 WASM 模块。属性面板要显示脚本对象的字段,序列化要保存脚本对象的状态——但编辑器运行时怎么描述一个「C# 的 Light」?它不认识 C# 的类型。

答案是类型桥:一张「脚本类型 ↔ 编辑器类型」的映射表。编辑器只认自己的类型名(Light、int、string),脚本侧的类型(CS_Light、CS_Int)经由映射翻译过来。TypeBridge 维护这张表:

  • register(m):登记一条映射——脚本类型名、编辑器类型名、可选 convert(把脚本值转成编辑器对象)。
  • convert(scriptType, raw):把脚本值经映射转换成编辑器对象;未注册返回 undefined。
  • editorTypeFor / scriptTypeFor:双向查询类型归属。

本章与站内既有内容的分工

6.1/6.2 定义编辑器侧类型知识;本节把它接到脚本侧。

主题站内既有内容本节做什么
类型描述符6.1 类型描述符与属性元数据描述符描述编辑器类型;桥把它们映射到脚本类型
编辑器标记6.2 编辑器标记与注解脚本字段的显示标记经桥查到编辑器字段
脚本热重载卷四 6 脚本热重载与域重载重载时桥的映射要保持稳定,第 4 卷深入

转换的唯一出处

TypeBridge 的关键纪律:脚本值 → 编辑器对象的转换,只在映射表里定义一次。属性面板显示脚本字段、序列化写盘、撤销记录,全都走 convert。任何地方想「临时自己转一下」都会造成转换规则多份、口径漂移——这正是 6.1「一份描述,多方消费」在多语言层上的投影。

convert 缺省是透传:CS_Name → string 这类同构映射不用写转换函数。真正需要转换的是异构的:脚本的整数、浮点、包装类型要落成编辑器的 number/boolean。

对标跨语言类型边界

引擎可核实的机制
Unity 6UnityEngine.Object 有托管对象与原生对象的关联,托管包装仍存在时原生对象可能已经销毁。 UnityEngine.Object
Unreal Engine 5C++ 声明通过 BlueprintCallable、BlueprintReadWrite 等标记向蓝图开放对应成员。 向蓝图公开 C++ 元素
Godot 4C# 与引擎的 Variant 互操作要求类型兼容,集合及数值有具体映射规则。 C# Variant
Cocos Creator 3.8原生环境提供 JSB,文档展示了用 SWIG 生成脚本与 C++ 绑定的流程。 JSB SWIG 教程

helio 的 TypeBridge 是 TypeScript 中的转换注册表。它模拟命名和转换规则,反查返回第一个登记的匹配项。脚本宿主、原生句柄生命周期和语言间调用仍需适配器实现。

可运行产物:6 条用例

下面 src/type-bridge.ts 层级 L1,完整可编译,命令 node --test test/06-3-multilang-script-type-bridge.test.ts:

// file: src/type-bridge.ts
// tier: L1
// verify: node --test test/06-3-multilang-script-type-bridge.test.ts
//
// 反射与类型注册(第六章):多语言脚本类型桥。
// 编辑器的脚本层(脚本语言/未来 WASM 模块)与编辑器运行时(TypeScript)
// 是两套类型系统。TypeBridge 维护「脚本类型 ↔ 编辑器类型」的双向映射,
// convert 把脚本值经映射转换成编辑器对象。编辑器描述脚本对象的字段时,
// 不需要知道脚本语言内部怎么表示,只认映射表。

export interface ScriptTypeMapping {
  scriptType: string;   // 脚本语言里的类型名
  editorType: string;   // 编辑器运行时类型名
  /** 把脚本值转换成编辑器对象;缺省为透传。 */
  convert?(raw: unknown): unknown;
}

export class TypeBridge {
  readonly #maps = new Map<string, ScriptTypeMapping>();

  register(m: ScriptTypeMapping): boolean {
    if (this.#maps.has(m.scriptType)) return false;
    this.#maps.set(m.scriptType, m);
    return true;
  }

  /** 脚本值 → 编辑器对象;未注册类型返回 undefined。 */
  convert(scriptType: string, raw: unknown): unknown {
    const m = this.#maps.get(scriptType);
    if (!m) return undefined;
    return m.convert ? m.convert(raw) : raw;
  }

  editorTypeFor(scriptType: string): string | undefined { return this.#maps.get(scriptType)?.editorType; }
  scriptTypeFor(editorType: string): string | undefined {
    for (const m of this.#maps.values()) if (m.editorType === editorType) return m.scriptType;
    return undefined;
  }

  /** 编辑器对象 → 脚本类型(反向转换由脚本侧完成,这里只查归属)。 */
  mappingFor(editorType: string): ScriptTypeMapping | undefined {
    for (const m of this.#maps.values()) if (m.editorType === editorType) return m;
    return undefined;
  }
}

convert 的设计只有一条规则:映射存在就走映射,不存在返回 undefined。返回 undefined 而不是抛错,是为了让调用方用 ?? 决定兜底——编辑器渲染脚本字段时,类型没登记就显示「未知类型」,而不是整个面板崩掉。

可运行产物:6 条用例

下面是 test/06-3-multilang-script-type-bridge.test.ts 层级 L1,命令 node --test test/06-3-multilang-script-type-bridge.test.ts:

// file: test/06-3-multilang-script-type-bridge.test.ts
// tier: L1
// verify: node --test test/06-3-multilang-script-type-bridge.test.ts
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { TypeBridge } from '../src/type-bridge.ts';

test('注册映射并双向查询类型', () => {
  const b = new TypeBridge();
  b.register({ scriptType: 'CS_Light', editorType: 'Light' });
  b.register({ scriptType: 'TS_Transform', editorType: 'Transform', convert: (raw) => ({ ...(raw as object), bridged: true }) });
  assert.equal(b.editorTypeFor('CS_Light'), 'Light');
  assert.equal(b.scriptTypeFor('Light'), 'CS_Light');
  assert.equal(b.scriptTypeFor('Transform'), 'TS_Transform');
});

test('convert 经映射转换,缺省映射透传', () => {
  const b = new TypeBridge();
  b.register({ scriptType: 'CS_Int', editorType: 'int', convert: (raw) => Number(raw) });
  b.register({ scriptType: 'CS_Name', editorType: 'string' }); // 无 convert
  assert.equal(b.convert('CS_Int', '42'), 42); // 字符串经转换变数字
  assert.equal(b.convert('CS_Name', 'helio'), 'helio'); // 透传
});

test('未注册类型 convert 返回 undefined', () => {
  const b = new TypeBridge();
  assert.equal(b.convert('NoSuch', 1), undefined);
  assert.equal(b.editorTypeFor('NoSuch'), undefined);
});

test('重复注册被拒绝,原映射保留', () => {
  const b = new TypeBridge();
  b.register({ scriptType: 'CS_Light', editorType: 'Light' });
  assert.equal(b.register({ scriptType: 'CS_Light', editorType: 'Light2' }), false);
  assert.equal(b.editorTypeFor('CS_Light'), 'Light');
});

test('mappingFor 按编辑器类型取回完整映射', () => {
  const b = new TypeBridge();
  b.register({ scriptType: 'CS_Light', editorType: 'Light', convert: (raw) => raw });
  const m = b.mappingFor('Light');
  assert.ok(m);
  assert.equal(m.scriptType, 'CS_Light');
  assert.equal(b.mappingFor('Ghost'), undefined);
});

test('一个编辑器类型只有一个脚本类型归属', () => {
  const b = new TypeBridge();
  b.register({ scriptType: 'A', editorType: 'Shared' });
  b.register({ scriptType: 'B', editorType: 'Shared' }); // 允许,但 scriptTypeFor 取第一个
  assert.equal(b.scriptTypeFor('Shared'), 'A');
});

本机实测(Node v26.5.0,node --test test/06-3-multilang-script-type-bridge.test.ts):tests 6、pass 6、fail 0,耗时约 40 ms。

第二条验证转换规则唯一出处:脚本的 '42' 经映射变成数字 42,无 convert 的映射透传——两条对照把「转换在映射表里定义一次」钉死;第三条验证未注册类型安全降级(undefined 而非崩溃);第四条是映射表的身份纪律(重复注册拒绝);第六条记录了一个现实局限——scriptTypeFor 对共享编辑器类型只返回第一个归属,多语言共存时的多对一在正文讨论。

小结

  • 类型桥 = 「脚本类型 ↔ 编辑器类型」的映射表,转换规则只在 convert 一处定义。
  • 编辑器描述脚本对象时不认识脚本语言,只认映射表——脚本类型知识集中、可扩展。
  • 未注册类型安全降级为 undefined,不会让面板崩溃。
  • helio 示例验证显式映射和单向值转换。实际跨语言调用还需运行时适配器。
  • 第 6 章结束。下一章把反射接回文档:文档模型与脏标记(深化)——场景文档操作 API、稳定 id 与脏传播、文档不变量校验。

阅读导航:上一节:6.2 编辑器标记与注解 · 下一节:7.1 场景文档操作 API 。

继续阅读

探索更多技术文章

浏览归档,发现更多关于系统设计、工具链和工程实践的内容。

全部文章 返回首页

「books」更多文章

  1. 《大规模分布式计算深入解析·向量数据库与 AI 检索内核》 · 目录与学习路径(45 篇)
  2. 《大规模分布式计算深入解析》15.3 自研向量引擎:可执行骨架与工程缺口
  3. 《大规模分布式计算深入解析》14.1 Milvus:组件架构、段管理与检索链路