首页 > 解决方案 > UE4中如何在关卡之间保存数据?

问题描述

我正在为我的游戏制作死亡系统。玩家获得 3 条生命,一旦 3 条生命全部耗尽,就会打开死亡屏幕。我遇到了一个问题。我需要拯救剩下的生命,这样我才能在关卡之间访问它。我以前从来没有保存过任何数据,所以我不知道该怎么做。这是我的死亡系统蓝图的图片。
蓝图

标签: unreal-engine4unreal-blueprint

解决方案


有几种方法可以在关卡之间保存数据。

1. 游戏实例

GameInstance is an object, that lives from start to an end of game process. It does not get removed on level switch. So, in order to pass some data, you can create your own GameInstance class, create the properties you need to pass between levels, and set your GameInstance class in Project Settings > Maps & Modes > Game Instance.

Before you switch level, just set variable in your game instance, and after switch, read it.

https://ibb.co/DR5zS5M

https://ibb.co/FYbyzfB

https://ibb.co/vx7s2SX

2. Using save system

With save system you simply save your progress and load it after switch. The idea here is to save all the data by using SaveGameToSlot before the level switch, and after it, load the data with LoadGameFromSlot.

虽然这不是一个优雅的解决方案。如果您仅在游戏开始时加载保存并将其与 GameInstance 正确结合,使用保存游戏对象作为进度容器,则效果非常好,因此您可以保存进度,并使用一个对象将其传递到不同的关卡。

https://ibb.co/qndzbp6

SaveGame 蓝图的创建方式与 GameInstance 蓝图相同。查看屏幕截图


推荐阅读