UE4网络同步属性笔记

GameMode只有服务端有,适合写游戏逻辑。PlayerController每个客户端拥有一个,并拥有主控权。GameState在服务端同步到全端。
CLIENT生成的Actor对其有Authority。
http://api.unrealengine.com/CHN/Gameplay/Framework/index.html
简单来讲 需要同步的操作写到Server,不需要同步的操作写到Client

变量同步

Replicated 复制模式
RepNotify:会创建一个变量改变通知的函数
属性复制条件介绍:http://api.unrealengine.com/CHN/Gameplay/Networking/Actors/Properties/Conditions/index.html

HasAuthority

对这个ACTOR是否有主控权限,Client对自己拥有的Pawn有Authority,Client生成的Actor也拥有Authority

Owner

给一个Actor设置拥有者,用于网络复制条件判断。源码里

Instigator

负责Destroy此Actor的Actor

UPROPERTY(ReplicatedUsing=OnRep_Owner)//属性复制,改变时调用函数onRep_Owner
AActor* Owner;

C++内写法

//.h
UFUNCTION(NetMulticast, reliable)
void Fun2();
//.cpp
void Cls1::Fun2_Implementation() {//这里要加_Implementation()
}//Fun2函数的实现被UHT(UnrealHeaderTool)使用了

相关代码

UObject::CallFunctionByNameWithArguments
UEngine::SetClientTravel

Actor的复制属性

OnlyRelevantToOwner 只和所有者相关

如果为真,则该角色(网络同步)仅与其所有者相关。如果在播放期间更改此标志,则所有非所有者信道都需要显式关闭。
如果这个Actor服务器只想让Actor的所有者得到同步信息,或者说,不想让整个地图的人都知道Actor的信息,只想让距离Actor的一定范围的人得到同步,又或者说,只想让某些付费玩家得到同步信息,怎么办?这时有一个叫“network relevant”的概念,也就是说,只有当一个Actor对于服务器是net relevant的时候,服务器才会同步信息给他。比如说:只想让Actor的所有者得到信息,我们可以设置Actor::bOnlyRelevantToOwner变量为true。

If true, this actor is only relevant to its owner. If this flag is changed during play, all non-owner channels would need to be explicitly closed

AlwaysRelevant

始终与网络相关(覆盖OnlyRelevantToOwner)

Always relevant for network (overrides bOnlyRelevantToOwner).

ReplicateMovement

如果是真的,复制移动/位置相关的属性,也必须是演员来复制。

If true, replicate movement/location related properties. Actor must also be set to replicate.

Net Load on Client

加载于地图加载期间

This actor will be loaded on network clients during map load

NetUseOwnerRelevancy

如果Actor有拥有者,调用业主的IsNetRelevantFor,GetNetPriority
覆盖IsNetRelevantFor,可自定义关联更新

If actor has valid Owner, call Owner's IsNetRelevantFor and GetNetPriority

Replicates 复写到远程机器

If true, this actor will replicate to remote machines

NetDormancy

Dormancy setting for actor to take itself off of the replication list without being destroyed on clients.
休眠设置,使演员从复制列表脱身而不被客户端损坏

  • DORM_Never 永不休眠
    This actor can never go network dormant.
  • DORM_Awake 当前醒着,可以关闭
    This actor can go dormant, but is not currently dormant. Game code will tell it when it go dormant.
  • DORM_DormantAll 休眠 全体
    This actor wants to go fully dormant for all connections.
  • DORM_DormantPartial 休眠部分
    This actor may want to go dormant for some connections, GetNetDormancy() will be called to find out which.
  • DORM_Initial 初始休眠
    This actor is initially dormant for all connection if it was placed in map.

NetCullDistanceSquared 网络剔除距离平方

最大距离的平方, 从客户视口来看, 这个演员是相关的, 将被复制。

Square of the max distance from the client's viewpoint that this actor is relevant and will be replicated.

NetUpdateFrequency 网络更新频率

How often (per second) this actor will be considered for replication, used to determine NetUpdateTime

MinNetUpdateFrequency 不经常更改时 速率

How often (per second) this actor will be considered for replication, used to determine NetUpdateTime

Net Priority 网络更新优先权

Priority for this actor when checking for replication in a low bandwidth or saturated situation, higher priority means it is more likely to replicate

Replicated Movement 移动复制

Velocity Quantization Level 量子化等级,把float后面的小数Round(四舍五入)

UE4 Actor复制流程

服务器遍历每一个actor查询是否参与属性复制

  • 如果actor是 初始休眠 的,则跳过
  • 通过AActor::netUpdateFrequency检查Actor是否需要更新
  • 如果AActor::bOnlyRelevantToOwner为真,那么通过AActor::IsRelevancyOwnerFor栓查Actor所属connection
  • 对于每一个通过最初检测的actor, 会调用AActor::PreReplication。

通过AActor::PreReplicatoin决定是否想要为连接复制这个属性。通过DorepLifeTime_Active _Override

  • 全部条件通过会加入到ConsiderList

对于每一个在considerList的Actor

  • 考虑是否休眠。
  • 如果当前还未创建channel,考虑客户端是否加载了这个Actor,考虑actor与当前connection的相关性,未通过则跳过
  • 将上述、前连接相关的actor加入列表,此时拥有了个与连接相关的需复制的actor列表,根据重要性对列表排序

对于每一个排序过后的Actor

  • 如果当前连接仍未加载actor所在的level,则关掉channel
  • 每一秒都通过调用AActor::IsNetRelevantFor来确定actor是否和当前连接相关
  • 如果5秒不相关,关闭连接。如果相关但没有channel,则新开一个channel

如果在任意时刻connection饱和

  • 对于剩余actors,如果相关小于1秒,强制下帧更新
  • 如果大于1秒,调用AActor::IsNetRelevantFor瘊定下一帧是否更新

对于通过了以上规则的actor

通过调用UChannel::ReplicateActor对Actor进行复制
如果是channel打开后的第一次Update那么需要特殊初始化一些信息
如果该链接拥有这个actor,则设置actor::ENetRole=ROLE_AuonomousProxy,否则actor::ENetRole=Role_SimulatedProxy

相关文章

http://www.cnblogs.com/Leonhard-/p/6511821.html
https://blog.csdn.net/xiejihao/article/details/80264148#commentBox
https://blog.csdn.net/u012999985/article/details/78384199

posted @ 2018-08-14 17:37  晨祷  阅读(5050)  评论(0编辑  收藏  举报