Net Role
AActor.Role描述了角色的網絡屬性,從而決定了rpc和replicated時的行為表現。這3個網絡Role屬性分別是:ROLE_Authority,ROLE_AutonomousProxy,ROLE_SimulatedProxy
ROLE_Authority,在服務器上的所有角色都是Authority屬性
ROLE_AutonomousProxy,客戶端上的本地角色
ROLE_SimulatedProxy,客戶端上的網絡角色
so
服務器上的所有角色都是Authority屬性,當前控制的角色可以用IsLocalControlled區分
客戶上當前控制的角色具有Autonomous屬性
客戶端上的遠程角色具有SimulatedProxy屬性
Actor 的 Role 和 RemoteRole 屬性
Net Mode
ENetMode AActor.GetNetMode()
NM_Standalone,
/** Standalone: a game without networking, with one or more local players. Still considered a server because it has all server functionality. */
NM_DedicatedServer,
/** Dedicated server: server with no local players. */
NM_ListenServer,
/** Listen server: a server that also has a local player who is hosting the game, available to other players on the network. */
NM_Client,
/**
* Network client: client connected to a remote server.
* Note that every mode less than this value is a kind of server, so checking NetMode < NM_Client is always some variety of server.
*/
Replicated Data
Actor中的Replicated數據自動復制到所有客戶端上
客戶端數據不能復制到服務器,只會在客戶端本地生效
so
如果數據定義為replicated,最好僅在server上進心更新,在client上只讀,避免引起不必要的混淆。
這樣判斷端的屬性:
在C++中判斷是否為服務器:Role == ROLE_Authority
在BP中判斷是否為服務器:HasAuthority
Replicated的數據在bp中set/get的時候會頭頂多頂2個小球
Replicated Data in C++
#include "Net/UnrealNetwork.h"
UPROPERTY(BlueprintReadOnly,Replicated)
float Health;
------
voidATestNetworkCharacter::GetLifetimeReplicatedProps(TArray< FLifetimeProperty> & OutLifetimeProps) const
{
Super::GetLifetimeReplicatedProps(OutLifetimeProps);
DOREPLIFETIME(ATestNetworkCharacter, Health);
}
如果不包含頭文件UnrealNetwork.h會報錯:
error C3861: 'DOREPLIFETIME': identifier not found
RPC
函數有3種replication方式
Multicast---在服務器調用,然后自動轉到客戶端
Server---被客戶端調用,僅在服務器執行;必須在有Net Owner的Actor上使用
Client---被服務器調用,僅在其所有者客戶端執行;必須在有Net Owner的Actor上使用
Net Owner
Actor如果是Player controller或被Player Controller所擁有,則此actor有Net Onwer
也就是說除了多播,rpc函數如果想調用成功必須有以下限制條件
Actor是一個Player Controller類型;
Actor被一個Player Controller所擁有;
您必須滿足一些要求才能充分發揮 RPC 的作用:
https://docs.unrealengine.com/latest/CHN/Gameplay/Networking/Actors/RPCs/index.html
它們必須從 Actor 上調用。
Actor 必須被復制。
如果 RPC 是從服務器調用并在客戶端上執行,則只有實際擁有這個 Actor 的客戶端才會執行函數。
如果 RPC 是從客戶端調用并在服務器上執行,客戶端就必須擁有調用 RPC 的 Actor。
xxx
xxx
xxx
xxx
xxx
xxx
xxx
xxx
xxx