๐Ÿ“œ โฌ†๏ธ โฌ‡๏ธ

Saving and loading the game in the Unreal Engine



Hello, my name is Dmitry. I create computer games on the Unreal Engine as a hobby. Today I will tell you how to save the game to disk and then download it from disk.

First you need to determine which variables you are going to save. This is done for C ++:

UPROPERTY(SaveGame) 

')
For blueprint:

image

After this, saving and loading is performed in this way:
Preservation:

 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 download differs only in what kind of archive we use FMemoryWriter - when saving and FMemoryReader when loading.
Object - This is an instance of the class of the variable values โ€‹โ€‹we want to save.
ObjectData - This is an array of bytes that we write to the file or read from the file.
ArIsSaveGame - This flag indicates that the archive used to save variables is marked SaveGame.
ArNoDelta - This flag should be set so that an error does not occur, which occurs if the value of the variable is first set in the class constructor and then on the class property panel. When saving a variable, the value of which is equal to the value in the constructor, and the subsequent loading, the value from the property panel will be loaded.

After all the values โ€‹โ€‹of variables are saved to the archive, you need to write this archive to a file. This is done like this:

Preservation:

 FFileHelper::SaveArrayToFile(Data, *SavePath) 

Loading:

 FFileHelper::LoadFileToArray(Data, *SavePath) 

In addition, to save disk space, you can save and load files in a compressed form:

Preservation:

 TArray<uint8> 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 need to create an intermediate archive FArchiveSaveCompressedProxy โ€” for data compression and FArchiveLoadCompressedProxy โ€” for data recovery.

Actually, that's all
โ†’ here is an example

Here, when you press s, all the actors are saved on the map that are inherited from the ISaveObject_StoryGraph class. When l is pressed, the corresponding load occurs, p is the output of variables on the screen i is the increment of variables

In addition, the plugin for editing quests also added the ability to save and load (the [- save,] - load keys),
โ†’ here it is .

You can read it here .

Source: https://habr.com/ru/post/319228/


All Articles