Hi Alonzo!
I’m a bit surprised with 's solution as, in C++, operator<< must return a valid object, usually the first parameter of the overloaded function, the object stream. Whether it is used in an Unreal environment or not should not change the rules of C++.
The empty return statement at the beginning is invalid as is yours.
This is how I would do it:
FORCEINLINE FArchive& operator<<(FArchive &Ar, UCustomSaveGame* SaveGameData )
{
if (SaveGameData)
{
Ar << SaveGameData->NumItemsCollected; //int32
Ar << SaveGameData->PlayerLocation; //FVector
Ar << SaveGameData->ArrayOfRotationsOfTheStars; //TArray<FRotator>
}
return Ar;
}
This permits streams like the one above to been daisy-chained as each << operator feeds on the output of its predecessor. The return value is what permits the archive to work as a stream. The code above is the same as:
Ar << SaveGameData->NumItemsCollected << SaveGameData->PlayerLocation << SaveGameData->ArrayOfRotationsOfTheStars;
So a return value is mandatory. If you test the validity of the pointer and the pointer is NULL, like in the code above, the stream is unaffected; nothing is added to Ar. It is simply passed along the next value to stream.
My problem with this code is that if a test fails then there is no way for a reader function to know whether the content was stored in the first place. I prefer passing objects by reference without any pointer conversion or testing at all.
FORCEINLINE FArchive& operator<<(FArchive &Ar, UCustomSaveGame& SaveGameData)
{
Ar << SaveGameData.NumItemsCollected; //int32
Ar << SaveGameData.PlayerLocation; //FVector
Ar << SaveGameData.ArrayOfRotationsOfTheStars; //TArray<FRotator>
return Ar;
}
But do not use **const ** on the reference because Ar does not use const itself and it wouldn’t help the reader function, of course.