开启Replicate

Actor开启Replicate

UE中, 对于需要ReplicateActor, 需要将AActor.bReplicates设置为true.

方法1: SpawnActor之后调用AActor.SetReplicates将其变成可Replicate.

方法2: 在构造函数中将AActor.bReplicates设置为true.

AActor.bReplicates控制的是哪里的流程?

对于动态创建的Actor, 在将其添加到ReplicationGraph的时候, 会检测其是否需要Replicate.

--UWorld::SpawnActor()
|--UWorld::AddNetworkActor()
| |--// 遍历所有NetDriver, 并将Actor添加到其中
| |--Driver->AddNetworkActor(Actor);
| | |--UNetDriver::AddNetworkActor()
| | | |--GetNetworkObjectList().FindOrAdd(Actor, this);
| | | |--// 向ReplicationGraph中添加NetworkActor
| | | |--ReplicationDriver->AddNetworkActor(Actor); // UReplicationGraph::AddNetworkActor
| | | | |--// 检测是否需要进行Replicate
| | | | |--IsActorValidForReplicationGather()
| | | | | |--AActor.GetIsReplicated() // 检测AActor的Replication
| | | | |--// 添加Actor对应的描述信息FGlobalActorReplicationInfo
| | | | |--FGlobalActorReplicationInfo& GlobalInfo = GlobalActorReplicationInfoMap.Get(Actor);
| | | | |--// 设置Dormant属性
| | | | |--GlobalInfo.bWantsToBeDormant = Actor->NetDormancy > DORM_Awake;
| | | | |--UReplicationGraph::RouteAddNetworkActorToNodes()
| | | | | |--// 针对ActorInfo.Actor->bAlwaysRelevant, 添加到AlwaysRelevantNode中
| | | | | |--AlwaysRelevantNode->NotifyAddNetworkActor(ActorInfo);
| | | | | |--// 针对ActorInfo.Actor->bOnlyRelevantToOwner, 添加到ActorsWithoutNetConnection中
| | | | | |--ActorsWithoutNetConnection.Add(ActorInfo.Actor);
| | | | | |--// 其他情况添加到GridNode中
| | | | | |--GridNode->AddActor_Dormancy(ActorInfo, GlobalInfo);
| | | | | | |--UReplicationGraphNode_GridSpatialization2D::AddActor_Dormancy
| | | | | | |--// 如果要想要休眠, 调用AddActorInternal_Static函数
| | | | | | |--AddActorInternal_Static(ActorInfo, ActorRepInfo, true);
| | | | | | | |--如果Actor没有Init, 则添加到UReplicationGraphNode_GridSpatialization2D.PendingStaticSpatializedActors中.
| | | | | | | |--否则添加到Static中
| | | | | | | |--UReplicationGraphNode_GridSpatialization2D.AddActorInternal_Static_Implementation()
| | | | | | | |--UReplicationGraphNode_GridSpatialization2D.AddActorInternal_Static_Implementation()
| | | | | | |--// 否则, 调用AddActorInternal_Dynamic
| | | | | | |--AddActorInternal_Dynamic(ActorInfo);
| | | | | | | |--// 放入了UReplicationGraphNode_GridSpatialization2D.DynamicSpatializedActors中
| | | | | | | |--DynamicSpatializedActors.Emplace(ActorInfo.Actor, ActorInfo);

ActorComponent开启Replicate

UE中, 对于需要ReplicateActorComponent, 需要将UActorComponent.bReplicates设置为true.

1
UActorComponent.SetIsReplicatedByDefault 

自定义UObject开启Replicate

参考:自定义UObject进行Replicate