> 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-triggers.md).

# 叙事触发器

在 Convai chatbot 组件上调用命名叙事触发器或动态上下文消息，以推进故事图并处理区段变化事件。

叙事触发器会推动角色的故事图从一个章节进入下一个章节。 `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) 。

### 选择合适的函数

| 目标                 | 函数                                  | 发送内容                                  |
| ------------------ | ----------------------------------- | ------------------------------------- |
| 使用控制面板中的触发器名称推进故事图 | **Invoke Narrative Design Trigger** | `trigger-message` ，并附带 `trigger_name` |
| 将运行时消息作为对话上下文暂存    | **Invoke Speech**                   | 通过以下方式发送动态上下文事件： `AddContextEvent`    |

在 **Invoke Narrative Design Trigger** 当设计时已知转场目标，且触发器名称是控制面板中的固定字符串时使用。若消息在运行时组装、角色应说出预设台词，或者你希望 Convai 将游戏状态文本作为对话上下文处理，而不是匹配控制面板中的触发器名称，则使用 **Invoke Speech** 。

**On Narrative Section Received** 仅当 Convai 通过一个 `BTResponse` 数据包确认章节变化后才会触发。调用任一函数时，此事件都不会触发。

### 调用命名触发器

**Invoke Narrative Design Trigger** (`InvokeNarrativeDesignTrigger`）会通过 `TriggerName` 将字符串发送给 Convai： `SendTriggerMessage`。Convai 会将该名称与控制面板中为当前章节配置的出站触发器进行匹配。

| 输入                     | 类型        | 说明                            |
| ---------------------- | --------- | ----------------------------- |
| `TriggerName`          | `FString` | 触发器名称。必须与控制面板中的配置完全一致（区分大小写）。 |
| `InGenerateActions`    | `bool`    | 存在于蓝图签名中。当前插件源码未应用此参数。        |
| `InReplicateOnNetwork` | `bool`    | 存在于蓝图签名中。当前插件源码未应用此参数。        |

如果 `TriggerName` 为空，插件会记录日志 `Invoke Narrative Design Trigger: TriggerName is missing` ，并直接返回，不会发送触发器。

{% hint style="warning" %}
从控制面板逐字复制触发器名称。蓝图字符串与控制面板中的 `trigger_name` 不匹配时，不会发生章节变化。
{% endhint %}

### 调用动态上下文消息

**Invoke Speech** (`ExecuteNarrativeTrigger`）会通过以下方式暂存动态上下文事件： `AddContextEvent` 与 `EC_RunLLMOption::Always`。它不会发送 `trigger-message` 数据包，也不会将其排入 `PendingTriggers`.

| 输入                     | 类型        | 说明                     |
| ---------------------- | --------- | ---------------------- |
| `TriggerMessage`       | `FString` | 作为上下文事件暂存的消息字符串。       |
| `InGenerateActions`    | `bool`    | 存在于蓝图签名中。当前插件源码未应用此参数。 |
| `InReplicateOnNetwork` | `bool`    | 存在于蓝图签名中。当前插件源码未应用此参数。 |

当角色应将消息作为上下文处理时，请使用纯文本 `TriggerMessage` ，例如 `Player completed the evacuation drill with 82 percent accuracy`。当角色应直接说出文本时，请将消息包裹在 `<speak>` 标签中，例如 `<speak>Attention: the evacuation route is now open.</speak>`.

如果 Convai 判断适合切换章节， **On Narrative Section Received** 仍会通过相同的 `BTResponse` 路径触发。

如果 `TriggerMessage` 为空，插件会记录 `Invoke Speech: TriggerMessage is missing` 并返回，不暂存事件。

### 处理章节切换事件

在聊天机器人组件上绑定 **On Narrative Section Received** (`OnNarrativeSectionReceivedEvent`），以便在 Convai 切换到新章节时作出响应。

| 输出引脚                 | 类型                         | 说明                    |
| -------------------- | -------------------------- | --------------------- |
| `ChatbotComponent`   | `UConvaiChatbotComponent*` | 接收到章节更新的组件。           |
| `NarrativeSectionID` | `FString`                  | 新活动章节的 `section_id` 。 |

要在蓝图中绑定：将 `UConvaiChatbotComponent` 引用拖入事件图表，然后从上下文菜单中选择 **Assign On Narrative Section Received** 。

如果某个特定章节需要运行自定义蓝图逻辑，请从 Convai 控制面板的 `section_id` 「 **叙事设计** 」中复制该章节的 ID。请使用 ID 值，而不是图表中显示的章节名称。在绑定的事件中，将传入的 `NarrativeSectionID` 并将其与复制的值进行比较。如果字符串匹配，则运行该分区的蓝图逻辑，例如开门、启用小组件或启动评估计时器。

### 待处理的命名触发器

当 **Invoke Narrative Design Trigger** 被调用且会话尚未连接时，插件会将调用存储在 `PendingTriggers`中。会话连接且动态上下文刷新完成后，待处理的触发器会按顺序重放。

{% hint style="warning" %}
调用 `Reset Dynamic Context` 会清除 `PendingTriggers`。如果组件在会话重新连接前被销毁，排队中的触发器也会被丢弃。 **Invoke Speech** 不会使用待处理触发器队列。
{% endhint %}

### 会话就绪

`bAutoInitializeSession` 在 `UConvaiChatbotComponent` 上的默认值为 `true`。启用后，组件会在 `StartSession` 在 **Begin Play**时调用。会话连接前调用的命名触发器会排入 `PendingTriggers` ，并在连接建立后重放。

如果 `bAutoInitializeSession` 为 `false`，请显式调用 `StartSession` ，以确保触发器能立即发送。需要确认角色数据已加载时，也可以绑定 **On Character Data Loaded** (`OnCharacterDataLoadEvent_V2`)，并在该事件中调用触发器。

### 后续步骤

{% content-ref url="/pages/453cd3ea71434b9c4a8a7da307779eeb39996f99" %}
[模板键](https://docs.convai.com/api-docs/zh/cha-jian-yu-ji-cheng/convai-unreal-engine-plugin/features/narrative-design/template-keys.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 %}

{% content-ref url="/pages/78fce7020677f54bda05ede0262a6996390ed909" %}
[叙事设计使用示例](https://docs.convai.com/api-docs/zh/cha-jian-yu-ji-cheng/convai-unreal-engine-plugin/features/narrative-design/narrative-design-usage-examples.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-triggers.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.
