
Saving and loading a game in Unreal Engine
- Tutorial

Hello, my name is Dmitry. I make computer games on the Unreal Engine as a hobby. Today I will tell you how to save the game to disk and then load it from disk.
First you need to determine which variables you are going to save; this is done for C ++:
UPROPERTY(SaveGame)
For blueprint:

After that, saving and loading is done in this way:
Saving:
FMemoryWriter MemoryWriter(ObjectData, true);
FObjectAndNameAsStringProxyArchive Ar(MemoryWriter, false);
Ar.ArIsSaveGame = true; //Set achive is savegame
Ar.ArNoDelta = true;
Object->Serialize(Ar);
Loading:
FMemoryReader MemoryReader(ObjectData, true);
FObjectAndNameAsStringProxyArchive Ar(MemoryReader, false);
Ar.ArIsSaveGame = true; //Set achive is savegame
Ar.ArNoDelta = true;
Object->Serialize(Ar);
As you can see, saving from downloading differs only in what archive we use FMemoryWriter for when saving and FMemoryReader for loading.
Object - This is an instance of the class of variable values which we want to save.
ObjectData - This is an array of bytes that we write to a file or read from a file.
ArIsSaveGame - This flag indicates that the archive is used to save variables marked as SaveGame.
ArNoDelta - This flag must be set so that there is no error that occurs if the value of a variable is first set in the class constructor and then on the class properties panel. When a variable is saved, the value of which is equal to the value in the constructor, and then loaded, the value from the properties panel will be loaded.
After all the values of the variables are saved in the archive, you need to write this archive to a file. This is done as follows:
Saving:
FFileHelper::SaveArrayToFile(Data, *SavePath)
Loading:
FFileHelper::LoadFileToArray(Data, *SavePath)
In addition, to save disk space, you can save and load files in compressed form:
Saving:
TArray CompressedData;
FArchiveSaveCompressedProxy Compressor(CompressedData, ECompressionFlags::COMPRESS_ZLIB);
// Compresed
Compressor << Data;
//send archive serialized data to binary array
Compressor.Flush();
FFileHelper::SaveArrayToFile(CompressedData, *SavePath)
Loading:
FFileHelper::LoadFileToArray(CompressedData, *SavePath)
// Decompress File
FArchiveLoadCompressedProxy Decompressor(CompressedData, ECompressionFlags::COMPRESS_ZLIB);
//Decompress
Decompressor << Data;
When using compression, we must create an intermediate archive FArchiveSaveCompressedProxy - for data compression and FArchiveLoadCompressedProxy - for data recovery.
Actually, that’s all
→ here’s an example
Here, when you press s, all actors on the map that are the heirs of the ISaveObject_StoryGraph class are saved. When l is pressed, respectively, loading occurs, p - display of variables on the screen i - increment of variables.
In addition, the plugin for editing quests also has the opportunity to save and load (keys [- save,] - load),
→ here it is .
You can read it here .