Use SaveGame specifier in UPROPERTY macro, it's also available in blueprints in details of selected variable.
Then you need these two simple functions in C++:
TArray<uint8> ObjectSaver(UObject* SaveObject)
{
TArray<uint8> ObjectData;
if (!IsValid(SaveObject))
{
return ObjectData;
}
FMemoryWriter MemoryWriter(ObjectData, true);
MySaveGameArchive MyArchive(MemoryWriter);
SaveObject->Serialize(MyArchive);
return ObjectData;
}
and
void ObjectLoader(UObject* LoadObject, TArray<uint8> ObjectData)
{
`if (ObjectData.Num() <= 0) return;`
`FMemoryReader MemoryReader(ObjectData, true);`
`MySaveGameArchive MyArchive(MemoryReader);`
`LoadObject->Serialize(MyArchive);`
}
They uses special struct which you also need:
struct MySaveGameArchive : public FObjectAndNameAsStringProxyArchive
{
`MySaveGameArchive(FArchive& InInnerArchive)`
`: FObjectAndNameAsStringProxyArchive(InInnerArchive, false)`
`{`
`ArIsSaveGame = true;`
`}`
};
With this solution you don't need to expand your save structure each time you have new variable to save. Save structure only needs TArray<uint8> for storing the result of ObjectSaver function. Just add SaveGame flag to your new variables that you need to save.