UE4網絡同步屬性筆記

GameMode只有服務端有,適合寫遊戲邏輯。PlayerController每一個客戶端擁有一個,並擁有主控權。GameState在服務端同步到全端。
CLIENT生成的Actor對其有Authority。
http://api.unrealengine.com/INT/Gameplay/Framework/GameMode/index.html
簡單來說 須要同步的操做寫到Server,不須要同步的操做寫到Clienthtml

變量同步

Replicated 複製模式
RepNotify:會建立一個變量改變通知的函數
屬性複製條件介紹:http://api.unrealengine.com/CHN/Gameplay/Networking/Actors/Properties/Conditions/index.htmlc++

HasAuthority

對這個ACTOR是否有主控權限,Client對本身擁有的Pawn有Authority,Client生成的Actor也擁有Authorityapi

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::SetClientTravelide

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 closedthis

AlwaysRelevant

始終與網絡相關(覆蓋OnlyRelevantToOwner).net

Always relevant for network (overrides bOnlyRelevantToOwner).code

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
http://www.javashuo.com/article/p-ppohxivk-md.html

相關文章
相關標籤/搜索