> 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/dynamic-context/dynamic-context-quick-start.md).

# 动态上下文快速入门

推送一个 **状态** 和一个 **事件** 从 Blueprint 向 Convai 角色推送，然后在你将其用于对话测试之前，确认更新已保存在本地。

{% embed url="<https://www.youtube.com/watch?v=zGIBCWSElXs>" %}
动态上下文演练
{% endembed %}

| 术语 | 含义        | 示例           |
| -- | --------- | ------------ |
| 状态 | 可被替换的当前事实 | `健康值` 为 `50` |
| 事件 | 会话中的一次性时刻 | `第 4 区触发警报`  |

### 前提条件

开始前，请确认：

* [ ] 已安装 Convai Unreal Engine 插件版本 <code class="expression">space.vars.unreal\_plugin\_version</code> 或更高版本，并且你的 API 密钥已配置。
* [ ] 某个关卡中包含一个带有 `Convai 聊天机器人` 组件（`UConvaiChatbotComponent`）并且具有 `CharacterID` 设置了……的
* [ ] 角色可以开始会话：要么 `bAutoInitializeSession` 为 `true`，要么你的 Actor 调用 `Start Session` 在 `BeginPlay`.
* [ ] 你熟悉在 Actor 上创建 Blueprint 图表。

### 推送状态更新

跟踪玩家的健康值，以便角色在被询问时可以引用它。

{% stepper %}
{% step %}

#### 打开角色蓝图

打开拥有该 `Convai 聊天机器人` 组件的 Actor 的蓝图——例如， `BP_ConvaiCharacter`。导航到 **事件图表**.
{% endstep %}

{% step %}

#### 添加一个测试入口

右键单击图表并添加一个 **自定义事件** 节点。将其命名为 `TestHealthUpdate`。你可以从关卡蓝图、触发体积或另一个 Actor 调用此事件，在 Play In Editor 期间触发更新。
{% endstep %}

{% step %}

#### 获取 Convai Chatbot 组件

将 `Convai 聊天机器人` 组件从 **Components** 面板拖到图表中，以创建一个 **获取** 引用节点。
{% endstep %}

{% step %}

#### 调用 Set Context State

从 `Convai 聊天机器人` 引用引脚拖出并搜索 **Set Context State**。将其连接到 `TestHealthUpdate` 事件执行引脚。

将节点引脚设置如下：

| 引脚     | 值     |
| ------ | ----- |
| **名称** | `健康值` |
| **值**  | `50`  |
| **应答** | `从不`  |

保持 `bFlushImmediately` 保持默认值（`false`).

`Should Respond = 从不` 在每次健康值变化时保存该值，而不会请求语音回复。
{% endstep %}

{% step %}

#### 确认本地跟踪器已接受该值

从 **Set Context State** 的输出执行引脚拖出并添加 **Get Context State Value**。设置 **名称** 到 `健康值`，然后打印 `OutValue` 以及布尔返回值，使用 **打印字符串**.

编译角色 Blueprint。运行时，返回值应为 `true` 和 `OutValue` 应显示为 `50`。这确认了本地跟踪器在任何网络刷新之前已接受该更新。
{% endstep %}
{% endstepper %}

### 推送事件更新

通知角色警报已被触发。事件记录一次性时刻；它们不会像状态值那样被替换。

{% stepper %}
{% step %}

#### 串联 Add Context Event

从 **Set Context State** 节点的输出执行引脚拖出。搜索 **Add Context Event** 并将其连接。
{% endstep %}

{% step %}

#### 配置事件

将 **Text** 引脚设为 `第 4 区触发警报`。设置 **应答** 到 `自动`.
{% endstep %}
{% endstepper %}

### 从游戏流程中触发更新

调用 `TestHealthUpdate` 在任何已可访问角色 Actor 的 Blueprint 图表中。

{% stepper %}
{% step %}

#### 获取角色 Actor 的引用

使用你项目中常用的 Blueprint 模式来引用拥有该 `Convai 聊天机器人` 组件的 Actor——例如，一个已放置的 `BP_ConvaiCharacter` 引用，或由你的生成逻辑设置的变量。
{% endstep %}

{% step %}

#### 在初始化期间调用 TestHealthUpdate

调用 `TestHealthUpdate` ，在角色 Actor 引用有效之后。对于基本测试，可从准备关卡的同一启动流程中调用它。
{% endstep %}

{% step %}

#### 编译 Blueprint

编译调用 `TestHealthUpdate`.
{% endstep %}
{% endstepper %}

### 在 Play In Editor 中测试

{% stepper %}
{% step %}

#### 进入 Play In Editor 并等待连接

在 Play In Editor 中运行关卡。等待角色会话连接——当 `bAutoInitializeSession` 启用时，会话会自动开始。

你的游戏图触发 `TestHealthUpdate`，并暂存状态更新和上下文事件。
{% endstep %}

{% step %}

#### 检查本地值并等待去抖窗口

检查来自……的调试输出 **Get Context State Value**。返回值应为 `true` 和 `OutValue` 应显示为 `50`.

默认情况下，插件会等待 `0.5` 秒（`ContextDebounceWindow`）在最后一次暂存更新之后再发送到 Convai。对于依赖此上下文的对话测试，至少等待一个去抖周期。
{% endstep %}

{% step %}

#### 在对话中验证

与角色对话或使用文本输入。询问 `我的健康状况如何？` 或 `设施里发生了什么？` 作为去抖窗口结束后的端到端运行时测试。具体回复措辞取决于角色配置。

如果角色没有引用这两个值中的任意一个，请参阅 [动态上下文故障排查](/api-docs/zh/cha-jian-yu-ji-cheng/convai-unreal-engine-plugin/features/dynamic-context/troubleshoot-dynamic-context.md).
{% endstep %}
{% endstepper %}

{% hint style="success" %}
如果 **Get Context State Value** 返回 `健康值 = 50` 并且你在测试前让去抖窗口经过，那么动态上下文管道的 Blueprint 端配置就是正确的。
{% endhint %}

### 下一步

阅读 [动态上下文的工作方式](/api-docs/zh/cha-jian-yu-ji-cheng/convai-unreal-engine-plugin/features/dynamic-context/how-dynamic-context-works.md) 以了解其思维模型，然后查看 [动态上下文使用示例](/api-docs/zh/cha-jian-yu-ji-cheng/convai-unreal-engine-plugin/features/dynamic-context/dynamic-context-usage-examples.md) 以获取更多 Blueprint 模式。

{% content-ref url="/pages/8ef7dcd046dad07f4afcb544f96d3beda6feb0ad" %}
[动态上下文的工作方式](/api-docs/zh/cha-jian-yu-ji-cheng/convai-unreal-engine-plugin/features/dynamic-context/how-dynamic-context-works.md)
{% endcontent-ref %}

{% content-ref url="/pages/4c32f5b1e84d192a1af40b32548e3cf5f7f90478" %}
[动态上下文使用示例](/api-docs/zh/cha-jian-yu-ji-cheng/convai-unreal-engine-plugin/features/dynamic-context/dynamic-context-usage-examples.md)
{% endcontent-ref %}

{% content-ref url="/pages/a80705d8480f2f63dc8b84ea2e437c9dac45b7c5" %}
[动态上下文 Blueprint 参考](/api-docs/zh/cha-jian-yu-ji-cheng/convai-unreal-engine-plugin/features/dynamic-context/dynamic-context-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 dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.convai.com/api-docs/zh/cha-jian-yu-ji-cheng/convai-unreal-engine-plugin/features/dynamic-context/dynamic-context-quick-start.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
