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