C++ get/set any variables dynamically using strings?

I’m working on a save system and I want to be able to get any variable and convert their name and value into a FString and when I load the string I want to set the variable value according to the value saved into the string. Is this possible? If so how can I implement this? If not is there any way to implement something similar? Please explain in detail. Thanks!

Hi. There are several things that can be useful:

Yeah but how would I implement these? After reading them I still don’t understand what most of the things do. How would I use these? Please explain in detail.

You were asking the ways to get and set props of classes by their names

  1. Reflection can give you all UProps of class with this

    for (TFieldIterator PropIt(GetClass()); PropIt; ++PropIt)
    {
    UProperty* Property = *PropIt;
    // Do something with the property
    }

UProperty class contains set and get methods and the name of the prop, other meta and so on

  1. Unreal Serialization serialize and deserialize classes. Depending on your task it may solve or may not solve your issues

  2. As for your own serialize/deserialize system - it is not very difficult, but usually people create their own systems because of something. It can be special optimization conditions, or external contracts, etc

Oh ok, I’ll be going with serialization. Quick question: Is there a way I could use ArIsSaveGame independently for every actor I’m serializing their variables, for example could I have Actor 1 to have ArIsSaveGame false and Actor 2 to True? Also could you explain the serialization progress? Serializing the variables and deserializing? Also what if the variable has been renamed, does the game still remember the variable or will it think it’s a new one? The approach I’m going for rn is serializing variable data into an array of uint8s or bytes and store it into a struct with other data and when I save the data I’ll serialize the struct and convert the bytes into text and store that inside a file. The links you posted were useful but not all of it was explained like what does what in detail. Apologies for being so much trouble, Thanks.

First of all, are you going to implement that in c++?

The serialization/deserialization of the data itself, yes in c++. My notifications whenever someone replies are a bit late sometimes. :slight_smile:

Ok, so:

I have tried to serialize some variables but I don’t think I did it right. How can I serialize all the variables from an actor with the save game flag ticked and convert it into bytes/uint8s? Also is there a way I can toggle between filtering variables using the save game flag and saving all the variables without it crashing unreal? Here is my code:

FBufferArchive Archive;
Archive.FlushCache();
Archive.Empty();
Archive.ArIsSaveGame = bUseSaveGameFlags;
GetOwner()->Serialize(Archive);
ActorStructure.Variables = Archive;

Yeah but in the pic it looks like serializing the variables manually, how could I serialize all the variables with the save game property and convert it into bytes?

I don’t mean to be rude and pressure but I’ve been on this for many hours for the past few days and I’ve been stuck on this. This has put a big road block with my save system as I have no idea to implement this by serializing all the variables with the save game property. If you could help please reply as soon as you can. Thanks!

Hi! There is some simple method. But it can be used only with UProps (so, you cant use this with classes that are not UObject nor UStructs). Mark all props that should be saved like this

UPROPERTY(SaveGame, EditAnywhere)
	int z = 1;

and after that you can save actor with this props of this actor with

	AMyActor* myActor = ... ///  poiter to actor you want to save
	TArray<uint8> data;
	FMemoryWriter MemoryWriter(data, true);
	MemoryWriter.ArIsSaveGame = true;
	myActor->Serialize(MemoryWriter);

and load with this

	TArray<uint8> dataCopy = data;
	FMemoryReader MemoryReader(dataCopy, true);
	MemoryReader.ArIsSaveGame = true;
	AMyActor* loadedActor = World->SpawnActor<AMyActor>(); // Create actor what you need
	loadedActor->Serialize(MemoryReader);

yeah I know it works but I’m trying to use the save game flags in BP. How would I get this working? It’s like the serialize() function doesn’t know that the variables have the save game flag ticked

it works but not for the save game flags in BP, How can I get this to work with the save game flag?

Hi! Try to use C++ classes and mark property like that (it worked for me on 4.23)

UPROPERTY(SaveGame, EditAnywhere)
     int z = 1;

Do you set this flag?

MemoryReader.ArIsSaveGame = true;

I found the problem. I think the problem is that for some reason, types like booleans and structures don’t get serialized even with the save game flag, but structs and floats and others do. How can I serialize these too? Also when I save it to text it comes out like this and I have no idea if the values got corrupted or thats how it is. Thanks!

SaveInterval FloatProperty
ç’Z€ªeK¬Ja…D|ˆ  @ None

Hi! I check some info, ustruct props are fine (only ustruct internal props with SaveSlot mark), bools are fine… But UObject pointers are not… Because you need to use another Archive for them…

I’m posting this as an answer because I can’t fit the entire text in a comment for some reason. Whenever I try to call Serialize() on an actor it ignores the variables even with the save game property/flag. But when I try to serialize variables manually by using the << operator it crashes and gives me an assertion failed error. Same whether ArIsSaveGame is set to true or false. I don’t know what I’m doing wrong. If you have any idea please help. Thanks!

Error:

Assertion failed: 0 [File:D:\Windows\Program Files (x86)\UE_4.25\Engine\Source\Runtime\Core\Public\Serialization/MemoryArchive.h] [Line: 56] 



0x00007ff847bc3b29 KERNELBASE.dll!UnknownFunction []
0x00007fffbb24ec66 UE4Editor-Core.dll!UnknownFunction []
0x00007fffbb252048 UE4Editor-Core.dll!UnknownFunction []
0x00007fffbafde52d UE4Editor-Core.dll!UnknownFunction []
0x00007fffbaf697c5 UE4Editor-Core.dll!UnknownFunction []
0x00007fffbaf6bb00 UE4Editor-Core.dll!UnknownFunction []
0x00007fff8ada7b7e UE4Editor-SkitzSaveSystem-6352.dll!FMemoryArchive::operator<<() [D:\Windows\Program Files (x86)\UE_4.25\Engine\Source\Runtime\Core\Public\Serialization\MemoryArchive.h:56]
0x00007fffbb077472 UE4Editor-Core.dll!UnknownFunction []
0x00007fffcd81019d UE4Editor-CoreUObject.dll!UnknownFunction []
0x00007fffcd811df2 UE4Editor-CoreUObject.dll!UnknownFunction []
0x00007fffcd6bbc35 UE4Editor-CoreUObject.dll!UnknownFunction []
0x00007fffcd6b9b7c UE4Editor-CoreUObject.dll!UnknownFunction []
0x00007fffcd79c884 UE4Editor-CoreUObject.dll!UnknownFunction []
0x00007fffcd79aeba UE4Editor-CoreUObject.dll!UnknownFunction []
0x00007fffcd79a9cb UE4Editor-CoreUObject.dll!UnknownFunction []
0x00007fffb3b7aaed UE4Editor-Engine.dll!UnknownFunction []
0x00007fffb4b7719e UE4Editor-Engine.dll!UnknownFunction []
0x00007fff8adabb54 UE4Editor-SkitzSaveSystem-6352.dll!UC_SaveComp::GetActorStructure() [D:\Documents\Unreal Projects\PluginDevelopment\Plugins\SkitzSaveSystem\Source\SkitzSaveSystem\Private\Objects\C_SaveComp.cpp:118]
0x00007fff8adaf30b UE4Editor-SkitzSaveSystem-6352.dll!UC_SaveComp::SaveActor() [D:\Documents\Unreal Projects\PluginDevelopment\Plugins\SkitzSaveSystem\Source\SkitzSaveSystem\Private\Objects\C_SaveComp.cpp:66]
0x00007fff8adbc571 UE4Editor-SkitzSaveSystem-6352.dll!UC_SaveComp::execSaveActor() [D:\Documents\Unreal Projects\PluginDevelopment\Plugins\SkitzSaveSystem\Intermediate\Build\Win64\UE4Editor\Inc\SkitzSaveSystem\C_SaveComp.gen.cpp:50]
0x00007fffcd5e0b34 UE4Editor-CoreUObject.dll!UnknownFunction []
0x00007fffcd8300ad UE4Editor-CoreUObject.dll!UnknownFunction []
0x00007fffcd8530e3 UE4Editor-CoreUObject.dll!UnknownFunction []
0x00007fffcd85571d UE4Editor-CoreUObject.dll!UnknownFunction []
0x00007fffcd823bba UE4Editor-CoreUObject.dll!UnknownFunction []
0x00007fffcd855362 UE4Editor-CoreUObject.dll!UnknownFunction []
0x00007fffcd85571d UE4Editor-CoreUObject.dll!UnknownFunction []
0x00007fffcd854c24 UE4Editor-CoreUObject.dll!UnknownFunction []
0x00007fffcd5e0b34 UE4Editor-CoreUObject.dll!UnknownFunction []
0x00007fffcd854753 UE4Editor-CoreUObject.dll!UnknownFunction []
0x00007fffb3b6afe5 UE4Editor-Engine.dll!UnknownFunction []
0x00007fff8adba599 UE4Editor-SkitzSaveSystem-6352.dll!AGlobalSaveActor::SaveAll() [D:\Documents\Unreal Projects\PluginDevelopment\Plugins\SkitzSaveSystem\Intermediate\Build\Win64\UE4Editor\Inc\SkitzSaveSystem\GlobalSaveActor.gen.cpp:128]
0x00007fff8adb094c UE4Editor-SkitzSaveSystem-6352.dll!USkitzSaveLibrary::SkitzSaveAll() [D:\Documents\Unreal Projects\PluginDevelopment\Plugins\SkitzSaveSystem\Source\SkitzSaveSystem\Private\Libraries\SkitzSaveLibrary.cpp:22]
0x00007fff8adbec35 UE4Editor-SkitzSaveSystem-6352.dll!USkitzSaveLibrary::execSkitzSaveAll() [D:\Documents\Unreal Projects\PluginDevelopment\Plugins\SkitzSaveSystem\Intermediate\Build\Win64\UE4Editor\Inc\SkitzSaveSystem\SkitzSaveLibrary.gen.cpp:201]
0x00007fffcd87a605 UE4Editor-CoreUObject.dll!UnknownFunction []
0x00007fffcd85571d UE4Editor-CoreUObject.dll!UnknownFunction []
0x00007fffcd854c24 UE4Editor-CoreUObject.dll!UnknownFunction []
0x00007fffcd5e0b34 UE4Editor-CoreUObject.dll!UnknownFunction []
0x00007fffcd854753 UE4Editor-CoreUObject.dll!UnknownFunction []
0x00007fffb3b6afe5 UE4Editor-Engine.dll!UnknownFunction []
0x00007fffb43fb5e8 UE4Editor-Engine.dll!UnknownFunction []
0x00007fffb43dc810 UE4Editor-Engine.dll!UnknownFunction []
0x00007fffb43f8adb UE4Editor-Engine.dll!UnknownFunction []
0x00007fffb8ceaf57 UE4Editor-UnrealEd.dll!UnknownFunction []
0x00007fffb95c37e6 UE4Editor-UnrealEd.dll!UnknownFunction []
0x00007ff654ca7800 UE4Editor.exe!UnknownFunction []
0x00007ff654cbba7c UE4Editor.exe!UnknownFunction []
0x00007ff654cbbb5a UE4Editor.exe!UnknownFunction []
0x00007ff654cce31d UE4Editor.exe!UnknownFunction []
0x00007ff654cd145a UE4Editor.exe!UnknownFunction []
0x00007ff84a9f7c24 KERNEL32.DLL!UnknownFunction []
0x00007ff84abacea1 ntdll.dll!UnknownFunction []

Component CPP:

FMemoryWriter MemoryWriter(ActorStructure.Variables);
MemoryWriter.ArIsSaveGame = bUseSaveGameFlags;
GetOwner()->Serialize(MemoryWriter);

FString Dir = FPaths::ProjectContentDir();
Dir.Append(TEXT("/Data.txt"));
FFileHelper::SaveArrayToFile(ActorStructure.Variables, *Dir);

Another Actor CPP:

FBufferArchive BinaryData;
BinaryData << InSaveInfo;

FFileHelper::SaveArrayToFile(BinaryData, *FileDirectory);

struct << Operator Definition:

friend FArchive& operator<<(FArchive& Ar, FSaveStructure& SaveInfo)
	{
		Ar << SaveInfo.SaveName;
		Ar << SaveInfo.FileDirectory;
		Ar << SaveInfo.AuthorName;
		Ar << SaveInfo.Thumbnail;
		Ar << SaveInfo.GameState;
		Ar << SaveInfo.GameVersion;
		Ar << SaveInfo.DateCreated;
		Ar << SaveInfo.Progress;

		return Ar;
	}