UE网络-WithCopy详解

WithCopy是什么

当属性发生变化时候, 怎么赋值. 即实现operator =操作函数重载.

自定义结构体指定WithCopy

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
USTRUCT()
struct FTestNetSerializeSharedPtrContainer
{
GENERATED_USTRUCT_BODY()

uint32 CounterUniqueID = 0;
TSharedPtr<FTestNetSerializeSharedPtrBase> Data;

/** Serialize the root motion sources and their states for this group */
bool NetSerialize(FArchive& Ar, UPackageMap* Map, bool& bOutSuccess);

bool Identical(const FTestNetSerializeSharedPtrContainer* Other, uint32 PortFlags) const;
bool operator==(const FTestNetSerializeSharedPtrContainer &Other) const;
bool operator!=(const FTestNetSerializeSharedPtrContainer& Other) const;
FTestNetSerializeSharedPtrContainer& operator=(const FTestNetSerializeSharedPtrContainer& Other);

void AddStructReferencedObjects(class FReferenceCollector& Collector);
};

template<>
struct TStructOpsTypeTraits< FTestNetSerializeSharedPtrContainer > : public TStructOpsTypeTraitsBase2< FTestNetSerializeSharedPtrContainer >
{
enum
{
WithNetSerializer = true,
WithIdenticalViaEquality = true,
WithCopy = true,
WithAddStructReferencedObjects = true
};
};

WithCopy映射到EStructFlags::STRUCT_CopyNative

构建UScriptStruct

在PreInit时候,构建UScriptStruct

WithCopy属性映射到EStructFlags::STRUCT_CopyNative

构建UScriptStruct, 通过模板, 指定STRUCT_CopyNative属性.

好了, 到此为止, 已经将我们指定的WithCopy映射到EStructFlags::STRUCT_CopyNative上了. 即UScriptStruct.StructFlags已经包含EStructFlags::STRUCT_CopyNative属性.

在需要复制的时候, 使用自定义函数

当属性初始化时

使用DefaultObject进行属性初始化.

当创建ShadowBuffer时候

当创建RepState时候

当属性发生变化时候,进行存储

还有很多情况, 这里就不一一列举了. 但是一定是调用UScriptStruct::CopyScriptStruct触发的.

进而调用该类型的复制重载操作符operator=