> 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/how-narrative-design-works.md).

# 叙事设计的工作原理

了解叙事设计故事图、推进区段的运行时管道，以及触发器和模板键如何塑造角色行为。

叙事设计为 Convai 角色提供结构化的故事图——由命名章节以及在章节间切换的触发器组成——在 Convai 控制面板中创作，并在运行时通过 `UConvaiChatbotComponent`执行。本页将介绍其思维模型和运行时流程。若要动手设置，请从 [叙事设计快速入门](https://docs.convai.com/api-docs/zh/cha-jian-yu-ji-cheng/convai-unreal-engine-plugin/features/narrative-design/narrative-design-quick-start.md).

### 故事图模型

叙事设计图包含三个可在 Unreal Engine 中使用的构建模块。

| 概念  | 说明                                                                                                            | 配置位置            |
| --- | ------------------------------------------------------------------------------------------------------------- | --------------- |
| 章节  | 一个命名的故事节点，包含用于塑造角色行为的 `objective` 。每个章节都有一个 `section_id`.                                                     | Convai 控制面板     |
| 触发器 | 一条命名边，用于激活目标章节。蓝图中的 `trigger_name` 必须与控制面板中的内容完全一致。                                                           | Convai 控制面板     |
| 模板键 | 聊天机器人组件中 `NarrativeTemplateKeys` 里的运行时键值对。插件会通过 `update-template-keys`将映射发送给 Convai。控制面板中的目标可以引用 `{key}` 占位符。 | 蓝图或 **详细信息** 面板 |

角色会停留在当前章节，直到 **调用叙事设计触发器** 激活另一个章节。该章节激活后，玩家的回答和章节中的决策会共同引导图中的下一次转换。有关决策结构体字段，请参阅 `FNarrativeDecision` 中的 [叙事设计蓝图参考](https://docs.convai.com/api-docs/zh/cha-jian-yu-ji-cheng/convai-unreal-engine-plugin/features/narrative-design/narrative-design-blueprint-reference.md) 。

在 Convai 控制面板中创作章节、触发器和入口行为。图表创作指南请参阅 [叙事设计 | Playground](https://docs.convai.com/api-docs/zh/convai-playground/character-customization/narrative-design.md) 。

### 命名触发器的运行时流程

从调用命名触发器到切换章节的标准流程：

1. 蓝图调用 **调用叙事设计触发器** 并传入一个 `TriggerName` 字符串。
2. 如果会话已连接，组件会发送一个 `trigger-message` 数据包，其中包含 `trigger_name` 发送给 Convai。如果连接断开，调用会排入 `PendingTriggers` 并在连接后重放。
3. Convai 会根据当前章节的出站触发器评估该触发器。匹配成功后，Convai 会激活目标章节。
4. 当前活动章节的 `objective` 会影响角色接下来的行为。
5. 对话继续时，玩家的回答会根据该章节的 `decisions` 进行评估。
6. Convai 会在发送章节更新时返回一个包含 `BTResponse` 数据包，其中带有 `narrative_section_id` 。
7. **收到叙事章节时** 会在聊天机器人组件上触发，并携带 `NarrativeSectionID`.

```mermaid
graph TD
    A["蓝图调用 Invoke Narrative Design Trigger"] --> B{"会话已连接？"}
    B -->|否| C["排入 PendingTriggers"]
    C --> D["会话连接后重放"]
    D --> E["向 Convai 发送 trigger_name"]
    B -->|是| E
    E --> F["Convai 根据当前章节评估触发器"]
    F --> G["目标章节变为活动章节"]
    G --> H["章节目标影响角色的回应"]
    H --> I["根据决策评估玩家回答"]
    I --> J["Convai 返回带有 narrative_section_id 的 BTResponse"]
    J --> K["触发“收到叙事章节时”事件"]
```

**收到叙事章节时** 仅当 Convai 通过 `BTResponse`确认章节变更时才会触发。调用触发器函数时不会触发。有关逐步调用触发器的说明，请参阅 [叙事触发器](https://docs.convai.com/api-docs/zh/cha-jian-yu-ji-cheng/convai-unreal-engine-plugin/features/narrative-design/narrative-triggers.md).

### 两个蓝图函数

`UConvaiChatbotComponent` 公开了两个相关函数，它们使用不同的运行时路径。

| 函数                                  | 发送给 Convai                            | 适用情况                                                          |
| ----------------------------------- | ------------------------------------- | ------------------------------------------------------------- |
| **Invoke Narrative Design Trigger** | `trigger-message` ，并携带 `trigger_name` | 转换目标在设计时已知，且触发器名称已在控制面板中固定。                                   |
| **Invoke Speech**                   | 通过 `AddContextEvent`                  | 动态上下文事件。消息在运行时生成，且你希望 Convai 将其作为对话上下文处理，而不是与控制面板中的触发器名称进行匹配。 |

命名触发器会排入 `PendingTriggers` 在断开连接时。 **调用语音** 不会使用该队列。如果 Convai 返回一个 `BTResponse` 并带有 `narrative_section_id`.

请参阅 [叙事触发器](https://docs.convai.com/api-docs/zh/cha-jian-yu-ji-cheng/convai-unreal-engine-plugin/features/narrative-design/narrative-triggers.md) 了解输入参数、待处理队列的行为，以及何时选择各个函数。

### 模板键

`NarrativeTemplateKeys` 是 `TMap<FString, FString>` 位于 `UConvaiChatbotComponent` 的 **Convai|NarrativeDesign**中。添加键与仪表板目标中的 `{key}` 占位符匹配的条目。

例如，如果仪表板目标写着 `Guide {PlayerName} through the safety inspection`，则添加 `PlayerName = "Rivera"` 到 `NarrativeTemplateKeys` 中，并确保在触发器推进到该章节之前完成添加。

插件会在会话连接时，以及你在已连接状态下设置 `NarrativeTemplateKeys` 时发送模板键。调用会推进到目标引用这些键的章节的触发器之前，请先设置这些键。请参阅 [模板键](https://docs.convai.com/api-docs/zh/cha-jian-yu-ji-cheng/convai-unreal-engine-plugin/features/narrative-design/template-keys.md) 了解设置和验证方法。

### 在仪表板中验证触发器名称

在蓝图中硬编码触发器名称之前，请在 Convai 仪表板中打开该角色的叙事图，并准确复制每个 `trigger_name` 。确认该触发器是角色起始章节的一条出边。请参阅 [叙事设计 | Playground](https://docs.convai.com/api-docs/zh/convai-playground/character-customization/narrative-design.md).

### 在运行时查询叙事数据

可以通过以下节点查询角色的完整章节和触发器列表： **Convai 获取叙事章节** 和 **Convai 获取叙事触发器** 位于 **Convai|REST API**下。使用这些节点验证触发器名称或填充游戏内界面，而不要将它们用作主要的故事推进方式。请参阅 [获取叙事数据](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/fc5915683cbb3a9ae3782e4de53c3288e9e3b7cc" %}
[叙事设计快速开始](https://docs.convai.com/api-docs/zh/cha-jian-yu-ji-cheng/convai-unreal-engine-plugin/features/narrative-design/narrative-design-quick-start.md)
{% endcontent-ref %}

{% content-ref url="/pages/ff19704aac20f7d6e4bf5c3214636ccbe4248151" %}
[叙事触发器](https://docs.convai.com/api-docs/zh/cha-jian-yu-ji-cheng/convai-unreal-engine-plugin/features/narrative-design/narrative-triggers.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/how-narrative-design-works.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.
