> For the complete documentation index, see [llms.txt](https://docs.convai.com/api-docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.convai.com/api-docs/zh/cha-jian-yu-ji-cheng/convai-unreal-engine-plugin/features/narrative-design/narrative-design-usage-examples.md).

# 叙事设计使用示例

用于线性场景推进、动态上下文事件、模板键注入以及叙事设计中触发器名称验证的 Blueprint 方案。

以下示例涵盖了培训模拟和互动体验中常见的叙事设计模式。每个示例都假设角色 Actor Blueprint 中包含一个 `UConvaiChatbotComponent` 其 `CharacterID` 引用了一个角色，该角色已在 Convai 控制面板中配置叙事图。

有关触发器调用的基础知识，请参阅 [叙事设计快速入门](https://docs.convai.com/api-docs/zh/cha-jian-yu-ji-cheng/convai-unreal-engine-plugin/features/narrative-design/narrative-design-quick-start.md)。有关 API 详情，请参阅 [叙事设计 Blueprint 参考](https://docs.convai.com/api-docs/zh/cha-jian-yu-ji-cheng/convai-unreal-engine-plugin/features/narrative-design/narrative-design-blueprint-reference.md).

### 线性场景推进

**场景：** 玩家在工业安全模拟中进入限制区域。进入该区域会触发控制面板中的触发器，从而激活下一个叙事部分。

**设置：**

1. 向区域 Actor 添加一个 `Box Collision` 组件。绑定 **On Component Begin Overlap**.
2. 在重叠事件中，从角色 Actor 引用获取 `UConvaiChatbotComponent` 。
3. 在 Convai 控制面板中，复制从当前部分引出的边上的触发器名称。
4. 调用 **Invoke Narrative Design Trigger** 并传入该触发器名称，例如 `enter_zone`.
5. 在聊天机器人组件上绑定 **On Narrative Section Received** ，这样 Blueprint 就能获知 Convai 何时确认目标部分。

**预期结果：** 玩家进入该区域时，Convai 会激活目标部分，角色将遵循新部分的目标。随后，玩家的回答可以满足该部分的决策条件，并推动叙事图进入后续部分。

***

### 为特定部分运行 Blueprint 逻辑

**场景：** 安全模拟中，只有当叙事图到达名为 `Evacuation route open`.

**设置：**

1. 在 Convai 控制面板中打开该角色，并转到 **Narrative Design**.
2. 选择目标部分并复制其 `section_id`。请使用 ID 值，而不是叙事图中显示的部分名称。
3. 在角色 Blueprint 中，在 **On Narrative Section Received** 上绑定 `UConvaiChatbotComponent`.
4. 将复制的 ID 存储在 Blueprint 字符串变量中，例如 `EvacuationRouteOpenSectionID`.
5. 添加一个 **Equal (String)** 比较，将传入的 `NarrativeSectionID` 与 `EvacuationRouteOpenSectionID`.
6. 将比较结果连接到 **分支** 节点。
7. 在 **True**时，运行该部分对应的蓝图逻辑，例如打开出口门、启用目标标记或启动计时器。

```
// 蓝图伪代码
接收到叙事部分时（NarrativeSectionID）
  → 字符串相等（NarrativeSectionID == EvacuationRouteOpenSectionID）
      True  → 打开出口门
      False → 不执行任何操作
```

**预期结果：** 无关部分的变更不会产生任何操作。当 Convai 发送复制的 `section_id`时，蓝图会运行与该部分关联的逻辑。

***

### 运行时消息或脚本语音

**场景：** 在医疗培训模拟中，玩家可以在复盘阶段自由发言。教员角色应对玩家的回答作出回应；如果各部分的决策与该回答相符，叙事还可以继续推进。

**设置：**

1. 从音频处理管线接收玩家语音转文字后的字符串。
2. 构造一个上下文字符串，将语音内容与相关游戏状态结合起来，例如 `玩家说：` + `SpeechText` + `。评估分数：` + `ScoreString`.
3. 调用 **调用语音** 在 `UConvaiChatbotComponent` 上，并将拼接好的字符串作为 `TriggerMessage`.
4. 当角色需要说出特定台词，而不是将消息视为一般上下文时，请使用 `<speak>` 标签，例如 `<speak>请前往复盘站。</speak>`.
5. 绑定 **接收到叙事部分时** 事件（如果响应需要触发特定部分的蓝图逻辑）。

**预期结果：** 角色会处理暂存的运行时消息。如果内容满足当前部分中的某项决策条件，Convai 就会推进故事图，并且 **接收到叙事部分时** 事件会以新的 `section_id`.

关于何时选择 **调用语音** 而不是 **调用叙事设计触发器**，请参阅 [叙事触发器](https://docs.convai.com/api-docs/zh/cha-jian-yu-ji-cheng/convai-unreal-engine-plugin/features/narrative-design/narrative-triggers.md).

***

### 驱动部分目标的模板键

**场景：** 企业入职模拟会在各章节目标中使用玩家的姓名和部门。所有员工共用同一张图，无需为每位员工单独创建章节。

**设置：**

1. 在 **Begin Play**时，从游戏状态或存档系统中获取玩家的姓名和部门。
2. 在 `UConvaiChatbotComponent`的 **Narrative Template Keys** 映射中为两者赋值，并确保在任何触发器触发之前完成：

```
// 蓝图伪代码
NarrativeTemplateKeys["PlayerName"]   = "Rivera"
NarrativeTemplateKeys["Department"]   = "Electrical"
```

3. 在 Convai 控制面板中，编写引用 `{PlayerName}` 和 `{Department}`.

**预期结果：** Convai 在此会话中评估的每个章节目标都会使用正确的玩家姓名和部门。连接期间更新映射，会将新值发送给 Convai，供下一次目标评估使用。

验证步骤请参见 [模板键](https://docs.convai.com/api-docs/zh/cha-jian-yu-ji-cheng/convai-unreal-engine-plugin/features/narrative-design/template-keys.md) 。

***

### 在会话就绪前拦截触发器

**场景：** 你的角色的 **Begin Play** 逻辑会立即执行，因此在会话启动前不能丢失命名触发器。

**设置：**

`bAutoInitializeSession` 在 `true` 时 `UConvaiChatbotComponent`默认会自动初始化，因此该组件会调用 `StartSession` 时 **Begin Play**。会话连接建立前调用的命名触发器会进入 `PendingTriggers` 队列，并在连接建立后自动重放。

如果 `bAutoInitializeSession` 为 `false`，请显式调用 `StartSession` ，或绑定 **角色数据已加载时** (`OnCharacterDataLoadEvent_V2`），并在该事件中调用触发器。

```
// 蓝图伪代码
事件：角色数据已加载（Success = true）
  → 调用叙事设计触发器（TriggerName = "start_briefing"）
```

**预期结果：** 会话建立后，命名触发器会送达 Convai。提前调用会进入队列，而不会在以下情况下失败 `bAutoInitializeSession` 为 `true`.

有关待处理队列的行为以及 `Reset Dynamic Context`，请参阅 [叙事触发器](https://docs.convai.com/api-docs/zh/cha-jian-yu-ji-cheng/convai-unreal-engine-plugin/features/narrative-design/narrative-triggers.md).

***

### 获取触发器，以便在调用前验证名称

**场景：** 你的蓝图中包含许多硬编码的触发器名称字符串。仪表板更新后，其中一个名称发生变化，导致静默失败。你希望在加载时捕获名称不匹配的问题。

**设置：**

1. 在 **Begin Play**时，调用 **Convai Fetch Narrative Triggers** 并传入角色的 `CharacterID`.
2. 在 **On Success** 引脚上，遍历 `Narrative Triggers` 数组。对于每个元素，提取 `trigger_name` 并将其添加到名为 `TSet<FString>` 的蓝图变量 `ValidTriggerNames`.
3. 在 **On Failure** 引脚上，打印一条警告并继续执行——如果数据不可用，则跳过触发器验证。
4. 每当调用 **Invoke Narrative Design Trigger**时，先检查预期的 `TriggerName` 是否包含在 `ValidTriggerNames`中。如果不在其中，就将该名称打印到 **输出日志**.

```
// 蓝图伪代码
Begin Play
  → Convai Fetch Narrative Triggers（CharacterId = CharacterID）
      On Success → For Each（Narrative Triggers）
                      → 将 trigger_name 添加到 ValidTriggerNames
      On Failure → 打印 "获取触发器失败 — 已跳过验证"

调用 Invoke Narrative Design Trigger 之前：
  → Branch：ValidTriggerNames 包含 TriggerName
      True  → 调用 Invoke Narrative Design Trigger
      False → 打印 "未知触发器：" + TriggerName
```

**预期结果：** 触发器名称不匹配的问题会在加载时显现，并附有清晰的日志消息。仪表板中的名称重命名后，可以在 **Begin Play** 时看到不匹配，而不是悄无声息地未能更改任何段落。

有关获取前置条件和验证的信息，请参阅 [获取叙事数据](https://docs.convai.com/api-docs/zh/cha-jian-yu-ji-cheng/convai-unreal-engine-plugin/features/narrative-design/fetching-narrative-data.md).

***

### 后续步骤

{% content-ref url="/pages/609986e3902c22d2027f232e5ffaaa507011c8a0" %}
[排查叙事设计问题](https://docs.convai.com/api-docs/zh/cha-jian-yu-ji-cheng/convai-unreal-engine-plugin/features/narrative-design/troubleshoot-narrative-design.md)
{% endcontent-ref %}

{% content-ref url="/pages/579f95b35d1b473357f37588e6fc915a03231497" %}
[叙事设计 Blueprint 参考](https://docs.convai.com/api-docs/zh/cha-jian-yu-ji-cheng/convai-unreal-engine-plugin/features/narrative-design/narrative-design-blueprint-reference.md)
{% endcontent-ref %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.convai.com/api-docs/zh/cha-jian-yu-ji-cheng/convai-unreal-engine-plugin/features/narrative-design/narrative-design-usage-examples.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
