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

Sorry for the delay. I have an update on what the result of my code is and what is going wrong. I’ve posted it as an answer because I couldn’t fit the entire text inside a comment. It’s been in moderation for the past few days. I ask you to be aware when the comment/answer is posted. Thanks for your patience!

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.