> 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/unreal-engine/guides/saving-and-loading-session.md).

# 保存和加载会话

### 简介

本指南说明如何使用 Unreal Engine 的 SaveGame 系统保存和加载 Session ID。相关步骤实现于 **ConvaiBaseCharacter 蓝图。** 按照本指南，您将能够在项目中无缝存储和检索 Session ID。

### 先决条件

开始之前，请确保您具备以下条件：

* 对 Unreal Engine 蓝图的基础知识。
* 熟悉 Unreal Engine 中的 SaveGame 系统。

### 步骤 1：创建 SaveGame 蓝图

要保存 Session ID，您需要创建一个用于存储数据的 SaveGame 蓝图。

1. **打开内容浏览器：**\
   前往 Unreal Engine 中的内容浏览器。
2. **创建 SaveGame 蓝图类：**
   * 在内容浏览器中右键单击。
   * 选择： **蓝图类 > 搜索 `SaveGame` > 选择 SaveGame**.
   * 为新蓝图命名 `BP_ConvaiSave` （您可以随意命名，但请确保蓝图引用中的名称保持一致。）
3. **添加一个变量以存储 Session ID：**
   * 打开 `BP_ConvaiSave` 蓝图。
   * 在变量面板中，单击 **+** 以创建新变量。
   * 按如下方式配置该变量：
     * **名称：** `SessionID`
     * **类型：** `字符串`
   * 编译并保存蓝图。

您的 SaveGame 对象现在已准备好存储 Session ID。

### 步骤 2：保存 Session ID

在 **说话结束时** 在……的事件中 **ConvaiBaseCharacter 蓝图**，请执行以下步骤以保存 Session ID：

1. **创建 Save Game Object：**
   * 添加 `创建 Save Game Object` 节点进行暂存。
   * 选择 `BP_ConvaiSave` 作为类。
2. **设置 Session ID：**
   * 使用 `设置 SessionID` 节点，将来自 Convai Chatbot 的 Session ID 赋值给 SaveGame 对象。
3. **保存游戏到槽位：**
   * 使用 `保存游戏到槽位` 节点，将 SaveGame 对象保存到名为 `“Session”` （您可以选择任意槽位名称，但请记住在加载已保存数据时使用相同的名称。）
   * 使用一个 `Branch` 节点检查操作是否成功。

<figure><img src="/files/2d253af6645c95f382645e34bcd643fe51ac4b08" alt=""><figcaption><p>此蓝图展示了当聊天机器人结束对话时，Session ID 如何保存到 SaveGame 槽位中。</p></figcaption></figure>

### 步骤 3：加载 Session ID

在 **开始游戏** 在……的事件中 **ConvaiBaseCharacter 蓝图**，请执行以下步骤以加载 Session ID：

1. **检查保存是否存在：**
   * 使用 `保存游戏是否存在` 节点，用于检查保存文件是否存在于 `“Session”` 槽位中。
2. **从槽位加载保存游戏：**
   * 如果保存存在，请使用 `从槽位加载游戏` 节点加载 SaveGame 对象。
3. **转换为 BP\_ConvaiSave：**
   * 使用 `转换为 BP_ConvaiSave` 节点以访问 `SessionID` SaveGame 对象中的变量。
4. **设置 Session ID：**

   * 将加载的 `SessionID` 重新赋值给 Convai Chatbot。

   <figure><img src="/files/86bdd0c80080ec227bbe1258b433bf4feda4987d" alt=""><figcaption><p>此蓝图演示了如何从 SaveGame 系统加载 Session ID，并将其重新设置回 Convai Chatbot。</p></figcaption></figure>

### 使用的关键蓝图节点

* **创建 Save Game Object：** 创建用于存储数据的 SaveGame 实例。
* **设置 SessionID：** 将 Session ID 赋值给 SaveGame 对象。
* **保存游戏到槽位：** 将创建的实例保存到磁盘。
* **保存游戏是否存在：** 检查指定槽位中是否存在保存文件。
* **从槽位加载游戏：** 从磁盘加载已保存的数据。
* **转换为 BP\_ConvaiSave：** 访问 SaveGame 对象中存储的数据。

### 结论

按照本指南，您现在已经在项目中使用 Unreal Engine 的 SaveGame 系统拥有了一个可用的 Session ID 保存和加载系统。此实现侧重于本地存储数据，但如有需要，您也可以扩展它以包含服务器端存储或自定义文件处理。

如需进一步定制或更高级的功能，请查阅 Unreal Engine 文档或社区教程。

如果您有任何问题，欢迎在 [Convai 开发者论坛](https://forum.convai.com/)！🎉😎


---

# 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/unreal-engine/guides/saving-and-loading-session.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.
