At this point I have a bunch of data for monsters, resources, and plants. Now I need a way to dump all that into a file and read it in later so that the player can save and load their game. This is using the Unreal Engine, which has a class decriptively labeled “SaveGame”. So I just have to create a new SaveGame object and fill it with the stuff I need to save right?
Well, I mean there are some complications. This post points out the problem and solution for saving Actors and Objects in Unreal Engine. The solution was described already on Epic’s own AnswerHub, but here I bring attention to it since it was kind of buried and took a while for me to find.
If you look at the Unreal Engine Documentation about saving game data you’ll see that the SaveGame object can be filled with data, and that data will then get put onto disk storage (or whatever storage is used on the target platform).
However, the SaveGame will only save certain primitive data types, such as strings/names, integers, and structs. If you try to save something like an UActor it won’t save. When you try to load the SaveGame from disk, the variable that was holding your Actor was will be empty (None).
Argh. Luckily, other people have already had this problem and figured out ways around it! Let’s look!
The explanation is this answer here about how Epic serializes Actors in Fortnite which talks about just serializing a UObject into an array of bytes and writing THAT to disk. So during normal play you use the Actors or UObjects holding the actual data you want. When the time comes to save or load data, the Actors are converted to/from byte arrays using Unreal’s built in Serialize functions.
What this means is that the normal SaveGame object needs to be modified:
- For each Actor or Object variable in the SaveGame an additional variable needs to be created to hold the serialized Byte Array representing that Object
- Before saving, each Actor/Object needs to be converted into a Byte Array, using Serialize
- After loading, each Actor/Object needs to be recreated using the Byte Array data, using Deserialize