# How to extend serialization of a USTRUCT?

**URL:** <https://forums.unrealengine.com/t/how-to-extend-serialization-of-a-ustruct/1350649>\
**Category:** C++\
**Tags:** Struct, serialization, unreal-engine, question, CPP\
**Created:** [October 29, 2023, 9:19am UTC](https://forums.unrealengine.com/t/how-to-extend-serialization-of-a-ustruct/1350649 "2023-10-29T09:19:53Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![heyx3](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@heyx3](https://forums.unrealengine.com/u/heyx3)\
**Post date:** [October 29, 2023, 9:19am UTC](https://forums.unrealengine.com/t/how-to-extend-serialization-of-a-ustruct/1350649/1 "2023-10-29T09:19:53Z")

</div>

If I have a USTRUCT with some fields that cannot be UPROPERTY’s, how can I manually extend serialization and replication to notice these fields?

It seems easy for UCLASS, but I can’t find much information on how it’s done for USTRUCT.

---

<div class="post-metadata">

**Author:** ![Chatouille](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/chatouille/32/1194622_2.png) [@Chatouille](https://forums.unrealengine.com/u/Chatouille)\
**Post date:** [October 30, 2023, 10:28am UTC](https://forums.unrealengine.com/t/how-to-extend-serialization-of-a-ustruct/1350649/2 "2023-10-30T10:28:36Z")

</div>

You need to declare a special template in C++ to notify the engine of your custom serializer(s).  
Something like this

```cpp
USTRUCT()
struct FMyStruct
{
    GENERATED_BODY()

    int32 A;
    double B;

    bool Serialize(FArchive& Ar)
    {
        Ar << A;
        Ar << B;
        return true;
    }

    bool NetSerialize(FArchive& Ar, class UPackageMap*, bool& bOutSuccess)
    {
        // optimize for network (example)
        Ar << A;
        if (Ar.IsLoading())
        {
            float Temp;
            Ar << Temp;
            B = (double)Temp;
        }
        else
        {
            float Temp = (float)B;
            Ar << Temp;
        }
        bOutSuccess = true;
        return true;
    }
};

template<> struct TStructOpsTypeTraits<FMyStruct> : public TStructOpsTypeTraitsBase2<FMyStruct>
{
    enum
    {
        WithSerializer = true,
        WithNetSerializer = true,
        WithNetSharedSerialization = true,
    };
};

```

Look into the declaration of TStructOpsTypeTraitsBase2 for all possible flags.

---

<div class="post-metadata">

**Author:** ![system](https://d3kjluh73b9h9o.cloudfront.net/original/4X/7/1/3/713c9d3f58553f0de89543d76a8a3a2779dc9fa4.png) [@system](https://forums.unrealengine.com/u/system)\
**Post date:** [November 29, 2023, 10:29am UTC](https://forums.unrealengine.com/t/how-to-extend-serialization-of-a-ustruct/1350649/3 "2023-11-29T10:29:23Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
