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.

docs: a guide for the Script Graph editor and the .esgraph asset

yhh4adf85946996
diff --git a/docs/astro/astro.config.mjs b/docs/astro/astro.config.mjs
index 870600155..cb5e9adad 100644
--- a/docs/astro/astro.config.mjs
+++ b/docs/astro/astro.config.mjs
@@ -246,6 +246,7 @@ export default defineConfig({
{ label: 'Physics', translations: { 'zh-CN': '物理' }, slug: 'gameplay/physics' },
{ label: '3D Physics', translations: { 'zh-CN': '3D 物理' }, slug: 'gameplay/physics-3d' },
{ label: 'Markers & Trigger Areas', translations: { 'zh-CN': '标记与触发区' }, slug: 'gameplay/markers' },
+ { label: 'Script Graphs', translations: { 'zh-CN': '脚本图' }, slug: 'gameplay/script-graphs' },
{
label: 'Gameplay AI',
translations: { 'zh-CN': '游戏 AI' },
diff --git a/docs/astro/src/content/docs/gameplay/script-graphs.mdx b/docs/astro/src/content/docs/gameplay/script-graphs.mdx
new file mode 100644
index 000000000..3eb6a8af2
--- /dev/null
+++ b/docs/astro/src/content/docs/gameplay/script-graphs.mdx
@@ -0,0 +1,113 @@
+---
+title: Script Graphs
+description: Author gameplay logic visually as a .esgraph asset — events, an exec wire, data wires, variables, callable graphs — and run it on an entity with ScriptGraphAgent.
+---
+
+A **script graph** is gameplay logic drawn as nodes and wires instead of typed. It is
+the visual sibling of [state machines and behavior trees](/docs/gameplay/ai/authoring/):
+the graph is a `.esgraph` asset, an entity runs it, and the leaves it calls are the
+same **actions** you register once in code. It suits the short, event-driven logic
+that is awkward as a state machine — *when the game starts, spawn this; when the key
+is pressed, branch on that*.
+
+The `.esgraph` file **is** the runtime definition: nothing compiles it ahead of time,
+so the graph the editor runs and the graph the package ships are the same bytes.
+
+## Creating and opening a graph
+
+1. In the **Content Browser**, **New → New Script Graph**. The new asset opens in a
+ document tab (double-click any `.esgraph` to reopen it).
+2. **Right-click the canvas** to add an event, then wire what it should do. An empty
+ graph says exactly that.
+3. Select the entity that should run it, add the **`ScriptGraphAgent`** component and
+ set its `graph` field to the asset.
+
+The asset is referenced by path and preloaded with the scene, so it is ready before
+the agent first ticks. Graphs run in **Play** (and in the exported game), in the
+`Update` schedule beside the state-machine interpreter.
+
+## Two kinds of wire
+
+| Wire | Carries | Direction |
+|---|---|---|
+| **Exec** (the control wire) | "run next" | *Pushed*: an event lights its entry node and the wire is walked depth-first. |
+| **Data** (number, boolean, string, entity, any) | a value | *Pulled*: a node reads its inputs by evaluating backwards up the wire when it runs. |
+
+Wires only connect compatible types. An input with nothing wired into it holds a
+**literal** you type on the node.
+
+Order is what the picture promises: a **Sequence** node finishes pin 0's whole chain
+before pin 1 starts. A pure node (no exec pins) feeding two pins of the same node is
+evaluated **once** per activation, so both pins see the same value; the next
+activation evaluates it again, which is what lets a **While** condition become false.
+
+## Adding nodes
+
+The **Node** button in the toolbar (or right-click on the canvas) lists nodes in
+groups:
+
+| Group | Nodes |
+|---|---|
+| **Events** | **Start**, **Every frame**, **Destroy**, **On event** (listens for a named entity event). Each is an entry point; a graph can have several. |
+| **Flow** | **Branch**, **Sequence** (configurable pin count), **While**, **Delay**. |
+| **Variables** | **Get variable**, **Set variable**. |
+| **Values** | **Number**, **Boolean**, **String** literals. |
+| **Queries** | Value-only nodes (no exec pin): arithmetic (Add, Subtract, Multiply, Divide, Absolute, Clamp, Random), comparisons (Less than, Greater than), **And**, **Join strings**, **Key pressed**, **Input axis**, **Find entity by name**, **Get property**, plus every registered **condition**. |
+| **Actions** | Nodes with an exec pin that do something: **Spawn entity**, **Destroy entity**, **Set property**, **Call graph**, plus every action you registered in code. |
+
+An action or condition you register with `registerAction` / `registerCondition` — the
+same ones state machines and behavior trees use — appears here with the ports its
+declaration gives it. The file stores only the verb's *name*, so an old graph is
+redrawn from the current declaration rather than keeping ports its verb no longer has.
+
+**Arrange** lays the nodes out left to right by what feeds what; positions are
+editor-only and ignored at runtime.
+
+## Variables
+
+The **Variables** panel on the canvas lists the graph's **Variables**, **Inputs** and
+**Outputs**, searchable, with how often each is read and written (or *unused*).
+
+- **+ Variable** declares a typed variable with a default. It is **declared by the
+ asset but valued per entity** — two entities running the same graph each keep their
+ own.
+- **Drag** a variable onto the canvas to read it, or choose **Get / Set** from the
+ right-click menu.
+- Rename or change a variable's type from its row. Deleting one that nodes still use
+ leaves those nodes on the canvas, without a variable, and asks first.
+
+## Callable graphs
+
+A graph becomes **callable** when you declare **+ In** (an input pin) or **+ Out** (an
+output pin) on it. Another graph then runs it with a **Call graph** node, handing
+values in and reading results back — the signature lives on the callee, so changing it
+redraws every caller.
+
+A call runs the callee **to completion, synchronously**, inside the caller's step. Each
+call site keeps its own frame for the callee, the callee spends the caller's step
+budget, and nesting is bounded — a graph that reaches itself through a cycle stops
+rather than overflowing the stack.
+
+## Safety limits
+
+Every node activation costs a **step**, and an instance has a budget per tick
+(4096 by default). A graph that loops forever — a **While** that never turns false —
+stops, names the node that exhausted the budget, and stays stopped, instead of
+freezing the editor. Calls nest at most 16 deep. A reference to a graph, prefab or
+verb that does not exist is reported once, not every frame.
+
+## From code
+
+Graphs can also be registered without an asset:
+
+```typescript
+import { registerScriptGraph, ScriptGraphAgent } from 'esengine/logic';
+
+registerScriptGraph('door-open', graphData); // a ScriptGraph object
+
+cmds.spawn().insert(ScriptGraphAgent, { graph: 'door-open' });
+```
+
+`ScriptGraphAgent.graph` takes either a registered name or a `.esgraph` path. See
+[AI → Authoring in the editor](/docs/gameplay/ai/authoring/) for the registry the
+actions come from.
diff --git a/docs/astro/src/content/docs/zh-cn/gameplay/script-graphs.mdx b/docs/astro/src/content/docs/zh-cn/gameplay/script-graphs.mdx
new file mode 100644
index 000000000..b33f8d3a4
--- /dev/null
+++ b/docs/astro/src/content/docs/zh-cn/gameplay/script-graphs.mdx
@@ -0,0 +1,87 @@
+---
+title: 脚本图
+description: 用节点和连线可视化编写玩法逻辑,保存为 .esgraph 资产——事件、执行线、数据线、变量、可调用的图——并用 ScriptGraphAgent 在实体上运行。
+---
+
+**脚本图**是用节点和连线画出来、而不是敲出来的玩法逻辑。它是[状态机与行为树](/docs/zh-cn/gameplay/ai/authoring/)
+的可视化兄弟:图是一个 `.esgraph` 资产,由实体运行,它调用的叶子就是你在代码里注册一次的那些**动作**。
+适合那种用状态机写起来别扭的短小、事件驱动的逻辑——*游戏开始时生成这个;按下某键时按条件分支*。
+
+`.esgraph` 文件**就是**运行时定义:没有任何提前编译,编辑器里跑的图和包里发布的图是同一份字节。
+
+## 创建与打开
+
+1. 在**内容浏览器**里 **新建 → 新建脚本图**。新资产会在文档标签里打开(双击任意 `.esgraph` 可重新打开)。
+2. **在画布上右键**添加一个事件,再连出它要做的事。空图会直接提示这一点。
+3. 选中要运行它的实体,添加 **`ScriptGraphAgent`** 组件,把它的 `graph` 字段设为该资产。
+
+资产按路径引用并随场景预加载,所以在代理第一次 tick 之前就已就绪。图在 **Play**(以及导出的游戏)中运行,
+位于 `Update` 阶段,和状态机解释器并列。
+
+## 两种连线
+
+| 连线 | 携带 | 方向 |
+|---|---|---|
+| **执行线**(控制线) | "接着运行" | *推*:事件点亮入口节点,沿线深度优先走下去。 |
+| **数据线**(数字、布尔、字符串、实体、任意) | 一个值 | *拉*:节点运行时,沿线向上求值来读取输入。 |
+
+连线只在兼容的类型之间建立。没有连线的输入保存一个你在节点上填的**常量**。
+
+顺序就是画面承诺的顺序:**顺序**节点先把第 0 个引脚的整条链走完,再开始第 1 个。
+一个纯节点(没有执行引脚)同时喂给同一节点的两个引脚时,每次激活只求值**一次**,两个引脚看到同一个值;
+下一次激活会重新求值,这正是**循环**条件能变成 false 的原因。
+
+## 添加节点
+
+工具栏的**节点**按钮(或在画布上右键)按组列出节点:
+
+| 分组 | 节点 |
+|---|---|
+| **事件** | **开始**、**每帧更新**、**销毁**、**监听事件**(监听具名的实体事件)。每个都是入口,一张图可以有多个。 |
+| **流程** | **分支**、**顺序**(引脚数可配)、**循环**、**延迟**。 |
+| **变量** | **读取变量**、**设置变量**。 |
+| **常量** | **数字**、**布尔**、**字符串**字面量。 |
+| **取值** | 只产出值的节点(没有执行引脚):算术(加、减、乘、除、绝对值、限制范围、随机数)、比较(小于、大于)、**与**、**拼接字符串**、**按键按下**、**输入轴**、**按名称找实体**、**读取属性**,以及所有已注册的**条件**。 |
+| **动作** | 带执行引脚、会做事的节点:**生成实体**、**销毁实体**、**设置属性**、**调用图**,以及你在代码里注册的所有动作。 |
+
+你用 `registerAction` / `registerCondition` 注册的动作和条件——状态机、行为树用的是同一批——会按它们的声明带着对应端口出现在这里。
+文件里只存动词的*名字*,所以旧图会按当前的声明重新绘制,不会保留动词早已没有的端口。
+
+**整理**按依赖关系把节点从左到右排开;位置只用于编辑器,运行时忽略。
+
+## 变量
+
+画布上的**变量面板**列出图的**变量**、**入参**和**出参**,可搜索,并显示每一项被读、被写的次数(或*未使用*)。
+
+- **+ 变量**声明一个带默认值的有类型变量。它**由资产声明、按实体取值**——两个运行同一张图的实体各自有自己的值。
+- 把变量**拖**到画布上即可读取,也可在右键菜单里选**读取 / 设置**。
+- 在变量那一行可以改名或更改类型。删除一个仍被节点使用的变量时会先确认;那些节点留在画布上,只是失去变量。
+
+## 可调用的图
+
+给一张图声明 **+ 入参**(输入引脚)或 **+ 出参**(输出引脚),它就变成**可调用**的。
+别的图用**调用图**节点运行它,传值进去、读回结果——签名在被调用方身上,改了签名,所有调用方都会跟着重绘。
+
+一次调用会在调用方的这一步里**同步运行到结束**。每个调用点为被调用图保留自己的帧,被调用图消耗的是调用方的步数预算,
+嵌套深度也有上限——经过环路再次到达自己的图会停下来,而不是把栈撑爆。
+
+## 安全上限
+
+每次节点激活消耗一**步**,一个实例每个 tick 有步数预算(默认 4096)。一张无限循环的图——一个永远不会变 false 的**循环**——
+会停下来、指出耗尽预算的那个节点,并保持停止,而不是把编辑器卡死。调用最多嵌套 16 层。
+引用了不存在的图、预制体或动词,只会报告一次,不会每帧刷屏。
+
+## 从代码使用
+
+图也可以不通过资产直接注册:
+
+```typescript
+import { registerScriptGraph, ScriptGraphAgent } from 'esengine/logic';
+
+registerScriptGraph('door-open', graphData); // 一个 ScriptGraph 对象
+
+cmds.spawn().insert(ScriptGraphAgent, { graph: 'door-open' });
+```
+
+`ScriptGraphAgent.graph` 既可以填注册名,也可以填 `.esgraph` 路径。动作所在的注册表见
+[AI → 在编辑器里编排](/docs/zh-cn/gameplay/ai/authoring/)。