Struct serialization

I’m writing my own SaveGame subsystem. I need to serialize and then deserialize an object. Everything works fine except for structs. I’ve tried to put a string and struct variables and marked them both as SaveGame properties. After saving and loading string variable restored it’s value, struct - no.

My serialization/deserialization algorithm:

TArray<uint8> USaveProfile::SerializeObject(UObject* target)
{
	TArray<uint8> bytes;
	FMemoryWriter memoryWriter(bytes, true);
	FObjectAndNameAsStringProxyArchive ar(memoryWriter, false);
	ar.ArIsSaveGame = true;
	ar.ArNoDelta = true;
	target->Serialize(ar);

	return bytes;
}

void USaveProfile::DeserializeObject(UObject* target, TArray<uint8> bytes)
{
	FMemoryReader memoryReader(bytes, true);
	FObjectAndNameAsStringProxyArchive ar(memoryReader, false);
	ar.ArIsSaveGame = true;
	ar.ArNoDelta = true;
	target->Serialize(ar);
}

Everything turned out to be very simple. I compared my code with the UnrealEngine SaveGameToSlot source code and noticed that there are no next lines:

ar.ArIsSaveGame = true;
ar.ArNoDelta = true;

I removed them from my code, compiled and it all worked.

Wow, this solved a big headache for me. Nothing was getting into my arrays and I couldn’t figure out what i was doing wrong; All examples seemed to set ArIsSaveGame in one way or another so I assumed it was needed to save out the SaveGame properties.

Thanks for posting your solution!

Also note that these flags are useful for desalinizing actors. I noticed that my actors were being broken upon being loaded from serialized data. Setting ArIsSaveGame be the destabilization step made the actors load the data correctly.

I know this thread is quite old but for people that are still looking for this answer I have some clarification.

Ar.ArIsSaveGame = true;

This flag is used to consider in serialization only the properties flagged with SaveGame, this reduce the work done on an object cause it can skip properties not meant to be saved, so for an optimization point of view it should always be checked if needed.
To make it work with a struct, every property of the structure needs to be set as SaveGame otherwise the number of elements in the struct will be saved but the content of those elements will not.

Example:

3 Likes