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

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;
	}

I’ve tried multiple different archives, methods but the serialize() function isn’t serializing the variables with the save game flag, even if ArIsSaveGame is set to false. Do you know what could be the problem? Also, check out the answer I posted, I tried serializing a struct but it crashes the game. Thanks!

EDIT: GOOD NEWS! I got the serializing part working by setting ArNoDelta to true. Ref: Serialization of default-constructed members? - C++ - Epic Developer Community Forums

But how would I serialize all the variables with or without the save game property? I want to be able to toggle whether or not to use the save game property or save all the variables. Setting ArIsSaveGame to false crashes the game when serializing. Thanks!

Hi there! With ArIsSaveGame to false every prop will be prepared to serialize. BUT with this you are risking to face crash/ The thing is that usual Archive classes in Unreal cant work with some types. For example, pointers to assets, TSubClass and so on…

Yeah, but I’ve recently found that using the object and name as string proxy archive works.

Yes, I am using the same thing - just deal with them as with string paths to assets…

Another question, How would you serialize structs directly? Whenever I try to use the << operator it crashes the game. Again I mean directly not by calling the serialize function on an object. I’m trying to serialize a structure into a text file. Thanks!

EDIT: I feel dumb. In the struct I had a UClass pointer and it was crashing because it was a pointer so I used the object and name as string archive and it worked.

To summarize: I defined the << operator for structs using
friend FArchive& operator<<(FArchive& Ar, FSomeStruct& Struct) { Ar << Struct.AnyProperty; Ar << Struct.AnotherProperty; return Ar; }
I used the FObjectAndNameAsStringProxyArchive class to serialize pointers without unreal crashing, of course, the pointers will be invalid when loaded back in. The include is #include "Serialization/ObjectAndNameAsStringProxyArchive.h" for others having trouble figuring out the include.
I used a memory writer to save the data and a memory reader to load the data back in. I also set ArIsSaveGame to either true or false to use the save game property and I set ArNoDelta to true to I think bypass a second check. Ref:
Serialization of default-constructed members? - C++ - Epic Developer Community Forums”. Example code to save the data is:
`
#include “Serialization/ObjectAndNameAsStringProxyArchive.h”
#include “Serialization/BufferArchive.h”

//Buffer Archive acts as an array and a memory writer, object and name archive for pointers.

void UMyClass::MyFunction(UObject* ObjectToSave, FString Directory)
{
TArray Data = {};
FMemoryWriter MemoryWriter(Data);
FObjectAndNameAsStringProxyArchive Ar(MemoryWriter, false);
Ar.ArIsSaveGame = bAnyBoolToDecideWhetherOrNotToUseSaveGameProperty;
Ar.ArNoDelta = true;

 ObjectToSave->Serialize(Ar);
 FFileHelper::SaveArrayToFile(Data, *Directory);

}
To Load the data back in you do the same thing but without the save to file and replacing FMemoryWriter to FMemoryReader And the data to load is the array of bytes/uint8s. You can get it by usingFFileHelper::LoadArrayFromFile()` Function.