> 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/template-keys.md).

# 模板键

在 chatbot 组件上填充 Narrative Template Keys 映射，使区段目标通过占位符替换引用实时游戏值。

模板键是存储在 `NarrativeTemplateKeys` 属性中的键值对，该属性属于 `UConvaiChatbotComponent`。会话连接后，插件会通过 `update-template-keys` 将此映射发送给 Convai。仪表板中的章节目标可以引用 `{key}` 占位符——在 `NarrativeTemplateKeys` 中填入对应的键，以便 Convai 接收图表所需的运行时值。

### 替换机制

在 Convai 仪表板中，编写章节目标时使用 `{key}` 占位符（例如 `{PlayerName}`）。在 Unreal Engine 中，在 Convai 评估该章节的目标之前，将匹配的键添加到 `NarrativeTemplateKeys` 中。

例如，某个章节目标如下：

```
引导 {PlayerName} 完成 {QuestName} 安全检查。
```

而 `NarrativeTemplateKeys` 包含：

| 键            | 值            |
| ------------ | ------------ |
| `PlayerName` | `Rivera`     |
| `QuestName`  | `electrical` |

Convai 收到的内容为：

```
引导 Rivera 完成电气安全检查。
```

确保 `NarrativeTemplateKeys` 中的每个键字符串与仪表板目标中的占位符名称逐字符完全匹配。

### 在“细节”面板中设置模板键

在关卡中选择角色 Actor，或打开角色蓝图。在 **组件** 面板中，选择 **Convai 聊天机器人**，然后在 **Convai|NarrativeDesign** 中展开 **细节** 面板。 `Narrative Template Keys` 字段的类型为 `TMap<FString, FString>`。对于设计时已知值的键，请添加对应条目。

### 在蓝图运行时设置模板键

为 `Narrative Template Keys` 属性赋值，该属性位于 `UConvaiChatbotComponent` 在蓝图中引用。像操作任何 `TMap` 一样直接修改此属性——可在游戏运行期间随时添加、移除或替换条目。

```
// 蓝图伪代码 — 通过 NarrativeTemplateKeys 属性上的 Set 节点赋值
NarrativeTemplateKeys["PlayerName"] = "Rivera"
NarrativeTemplateKeys["QuestStatus"] = "completed"
```

为此属性赋值时，内部会调用 `UpdateNarrativeTemplateKeys` 。如果聊天机器人会话已连接，插件会向 Convai 发送一条 `update-template-keys` 消息，其中包含当前映射。

{% hint style="info" %}
`UpdateNarrativeTemplateKeys` 标记为 `BlueprintInternalUseOnly` ，不能作为可调用的蓝图节点使用。请直接赋值给 **叙事模板键** 属性。
{% endhint %}

### 键的应用时机

Convai 会在评估当前章节的目标时应用模板键，而不是在触发器触发时应用。对于新章节，请在调用 **调用叙事设计触发器** 之前设置好键，以便在评估目标章节的目标时这些键已生效。

插件还会在会话连接时发送 `NarrativeTemplateKeys` （`OnAttendeeConnected`）。如果需要从第一个章节开始就能使用这些值，请在 **开始游戏** 时填充映射，并确保在触发器触发之前完成。

### 验证模板键

在调用会推进到目标包含 `{key}` 令牌的章节的触发器之前：

1. 在蓝图中打印或检查 `NarrativeTemplateKeys` ，或查看 **详情** 面板，确认其中包含预期的键值对。
2. 逐字符比对每个键字符串与仪表板目标中的 `{key}` 令牌。
3. 触发器触发后，确认角色的行为反映了替换后的值。如果响应中 `{key}` 令牌仍以原样出现，最可能的原因是键名不匹配或映射为空。请参阅 [排查叙事设计问题](https://docs.convai.com/api-docs/zh/cha-jian-yu-ji-cheng/convai-unreal-engine-plugin/features/narrative-design/troubleshoot-narrative-design.md).

### 后续步骤

{% 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 %}

{% 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 %}

{% 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 %}


---

# 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/template-keys.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.
