How to find out binary size of a UStruct?

k_a_s_s_k_a_s_s
Thu, 08 Jul 2021 10:54:22 +0000
Editor Scripting

For a given dynamically-sized serializable USTRUCT, i.e.

USTRUCT()
FSomeStruct
{
   GENERATED_BODY()

   UPROPRETY()
   TArray<float> SomeData;

   UPROPRETY()
   FString SomeString;

   friend FArchive& operator<< (FArchive& Ar, FSomeStruct& Struct) {
        Ar << Struct.SomeData;
        Ar << Struct.SomeString;
        return Ar;
   }
};

What is the correct way to get the binary size (in bytes) of an instance of this USTRUCT? For example, the instance has 15 floats in the SomeData array, and a string with 20 characters.

I mean the size it would occupy in an FArchive when serialized with the << operator, like an FBufferArchive.

I understand that a reliable way would be to actually serialize it into a FBufferArchive, and then see the difference in the container’s size before/after, but I don’t want/need to actually serialize it and waste memory and instructions. I just need to know the size.

I assume there is some easy way of knowing this?

I have found the UStruct::GetStructureSize() function, but I don’t know if it’s what I need, and apart from that I can’t seem to get the FSomeStruct as a UStruct, to actually be able to call it.

I hope someone here can help out with this, it would be much appreciated!

2 posts - 2 participants