How to automatic serialize instance of USTRUCT variable via reflection system if all properties are marked with UPROPERTY to array of bytes (TArray)? Any function or module inside engine maybe available about which i do not know?
I’m not sure if this will answer your question, but one way of getting your USTRUCT into an array of bytes can be done like so:
USTRUCT()
struct FSaveMyStruct
{
GENERATED_USTRUCT_BODY()
UPROPERTY()
int32 AnyInt32;
};
FSaveMyStruct MyStructInstance;
MyStructInstance.AnyInt32 = 77;
FBufferArchive Buffer(true);
FSaveMyStruct::StaticStruct()->SerializeBin(Buffer, &MyStructInstance);
TArray<uint8> Bytes = Buffer;
Since FBufferArchive inherits from TArray you could just use any TArray
functions on Buffer. Hope this helps.
Oh, dunno how I missed it. I saw there is JSON automatic serializer and wanted to implement my own binary. Now I don’t need to do it, Thank you!
Incase anyone else is looking. To serialize a UStruct in 5.4:
Header file to show the USTRUCT
USTRUCT(BlueprintType, Blueprintable)
struct FGGB_SaveData
{
GENERATED_USTRUCT_BODY()
public:
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "GothGirl|Build")
int Version{ 1 };
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "GothGirl|Build")
TSubclassOf<AActor> Class;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "GothGirl|Build")
FString Data;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "GothGirl|Build")
FTransform Transform;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "GothGirl|Build")
TArray<FTransform> HISM;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "GothGirl|Build")
TArray<bool> IsSlope;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "GothGirl|Build")
TArray<bool> IsInside;
// This method is responsible for custom serialization
friend FArchive& operator<<(FArchive& Ar, FGGB_SaveData& MyStruct)
{
// Serialize each member variable manually
Ar << MyStruct.Version;
if (Ar.IsSaving())
{
FString PathStr = MyStruct.Class ? MyStruct.Class->GetPathName() : FString();
Ar << PathStr;
}
else if (Ar.IsLoading())
{
FString PathStr;
Ar << PathStr;
if (!PathStr.IsEmpty())
{
MyStruct.Class = LoadClass<AActor>(nullptr, *PathStr);
}
else
{
MyStruct.Class = nullptr;
}
}
//Ar << MyStruct.Class;
Ar << MyStruct.Data;
Ar << MyStruct.Transform;
Ar << MyStruct.HISM;
Ar << MyStruct.IsSlope;
Ar << MyStruct.IsInside;
return Ar;
}
};
CPP File to serialize and deserialize. Currently using this to upload data to a cloud service.
#include "Misc/Base64.h"
FString UGGBHelper::GGB_SerializeSaveData(FGGB_SaveData Data)
{
TArray<uint8> BinaryData;
FMemoryWriter MemoryWriter(BinaryData, true);
MemoryWriter.SetIsSaving(true);
MemoryWriter << Data;
return FBase64::Encode(BinaryData);
}
FGGB_SaveData UGGBHelper::GGB_DeSerializeSaveData(FString Base64Str)
{
FGGB_SaveData Data;
if (!Base64Str.IsEmpty() )
{
TArray<uint8> BinaryData;
BinaryData.SetNumUninitialized(Base64Str.Len());
if (FBase64::Decode(Base64Str, BinaryData))
{
FMemoryReader MemoryReader(BinaryData, true);
MemoryReader.SetIsLoading(true);
MemoryReader << Data;
UE_LOG(GothGirlMod, Log, TEXT("UGGBHelper::GGB_SerializeSaveData Base64Str.Len(%i) Data.HISM[%i] Data.Class(%s)"), Base64Str.Len(), Data.HISM.Num(), IsValid(Data.Class) ? *Data.Class->GetFName().ToString() : TEXT("nullptr"));
}
else
{
UE_LOG(GothGirlMod, Error, TEXT("UGGBHelper::GGB_SerializeSaveData FBase64::Decode FAILED!!!"));
}
}
return Data;
}
2 Likes