Net Role
AActor.Role描述了角色的網(wǎng)絡(luò)屬性,從而決定了rpc和replicated時的行為表現(xiàn)。這3個網(wǎng)絡(luò)Role屬性分別是:ROLE_Authority,ROLE_AutonomousProxy,ROLE_SimulatedProxy
ROLE_Authority,在服務(wù)器上的所有角色都是Authority屬性
ROLE_AutonomousProxy,客戶端上的本地角色
ROLE_SimulatedProxy,客戶端上的網(wǎng)絡(luò)角色
so
服務(wù)器上的所有角色都是Authority屬性,當前控制的角色可以用IsLocalControlled區(qū)分
客戶上當前控制的角色具有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數(shù)據(jù)自動復(fù)制到所有客戶端上
客戶端數(shù)據(jù)不能復(fù)制到服務(wù)器,只會在客戶端本地生效
so
如果數(shù)據(jù)定義為replicated,最好僅在server上進心更新,在client上只讀,避免引起不必要的混淆。
這樣判斷端的屬性:
在C++中判斷是否為服務(wù)器:Role == ROLE_Authority
在BP中判斷是否為服務(wù)器:HasAuthority
Replicated的數(shù)據(jù)在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
函數(shù)有3種replication方式
Multicast---在服務(wù)器調(diào)用,然后自動轉(zhuǎn)到客戶端
Server---被客戶端調(diào)用,僅在服務(wù)器執(zhí)行;必須在有Net Owner的Actor上使用
Client---被服務(wù)器調(diào)用,僅在其所有者客戶端執(zhí)行;必須在有Net Owner的Actor上使用
Net Owner
Actor如果是Player controller或被Player Controller所擁有,則此actor有Net Onwer
也就是說除了多播,rpc函數(shù)如果想調(diào)用成功必須有以下限制條件
Actor是一個Player Controller類型;
Actor被一個Player Controller所擁有;
您必須滿足一些要求才能充分發(fā)揮 RPC 的作用:
https://docs.unrealengine.com/latest/CHN/Gameplay/Networking/Actors/RPCs/index.html
它們必須從 Actor 上調(diào)用。
Actor 必須被復(fù)制。
如果 RPC 是從服務(wù)器調(diào)用并在客戶端上執(zhí)行,則只有實際擁有這個 Actor 的客戶端才會執(zhí)行函數(shù)。
如果 RPC 是從客戶端調(diào)用并在服務(wù)器上執(zhí)行,客戶端就必須擁有調(diào)用 RPC 的 Actor。
xxx
xxx
xxx
xxx
xxx
xxx
xxx
xxx
xxx