目录
一、网络模式类型
NM_Standalone | 仅是一个可以单独运行的程序。 |
NM_DedicatedServer | 仅是一个服务器。 |
NM_ListenServer | 即是一个服务器,又是一个客户端。允许其它客户端远程接入。 |
NM_Client | 仅是一个客户端,需要接入远端的服务器。 |
NM_DedicatedServer、NM_ListenServer、NM_Client都属于Client-Server模式。
可通过AActor.GetNetMode( )获取。
二、Client-Server模式下的使用方式
1.命令行启动方式
Type | Command |
---|---|
Listen Server |
|
Dedicated Server |
|
Client |
|
2.客户端接入后会依次调用如下方法
AGameModeBase::PreLogin 可以在此判断是否要拒绝客户端连接
AGameModeBase::Login 会创建PlayerController,并执行APlayerController::BeginPlay
AGameModeBase::PostLogin 在此之后可以调用PlayerController上的RPC函数
3.客户端和服务端的角色
on server:
Role == ROLE_Authority // server
RemoteRole == ROLE_SimulatedProxy/ROLE_AutonomousProxy // clienton client:
Role == ROLE_SimulatedProxy/ROLE_AutonomousProxy // client
RemoteRole == ROLE_Authority // server
三、数据复制方式
1.属性复制
属性必须定义在AActor中,属性更新频率的参数NetUpdateFrequency,如果打开了命令行参数
net.UseAdaptiveNetUpdateFrequency,则NetUpdateFrequency和MinNetUpdateFrequency会同时起作用。
UPROPERTY(Replicated)
float Health;
#include "UnrealNetwork.h"
// ...
void AYourCharacter::GetLifetimeReplicatedProps(TArray< FLifetimeProperty > & OutLifetimeProps) const
{
Super::GetLifetimeReplicatedProps(OutLifetimeProps);
DOREPLIFETIME(AYourCharacter, Health);
}
或
UPROPERTY( ReplicatedUsing=OnRep_Health )
float Health;
UFUNCTION( )
void OnRep_Health( );
void MyCharacter::GetLifetimeReplicatedProps( TArray< FLifetimeProperty > & OutLifetimeProps ) const
{
Super::GetLifetimeReplicatedProps( OutLifetimeProps );
DOREPLIFETIME( MyCharacter, Health);
}
void MyCharacter::OnRep_Health( )
{
// Handle health changed
}
2.Actor&Component复制
C++:
AActorComponent::SetIsReplicated(true)
AActor::SetReplicates(true
Blueprint:
属性面板中设置Replicates,或调用SetIsReplicated节点
3.RPC
// server to a client
UFUNCTION( Client )
void Function(...)
void Function_Implementation(...)
// a client to server
UFUNCTION( Server )
void Function(...)
void Function_Implementation(...)
void Function_Validate(...)
// server to clients
UFUNCTION( NetMulticast )
void Function(...)
void Function_Implementation(...)
参考文档
Documentation Home -> Engine Features -> Networking and Multiplayer -> Networking Overview
Documentation Home -> Engine Features -> Networking and Multiplayer -> Multiplayer in Blueprints
Documentation Home -> Engine Features -> Networking and Multiplayer -> Client-Server Model
Documentation Home -> Engine Features -> Networking and Multiplayer -> Actor Replication
Documentation Home -> Engine Features -> Networking and Multiplayer -> Actor Replication -> Property Replication
Documentation Home -> Engine Features -> Networking and Multiplayer -> Actor Replication -> RPCs
Documentation Home -> Gameplay Guide -> How to Replicate Actors
Documentation Home -> Gameplay Guide -> Replicating Variables
Documentation Home -> Gameplay Guide -> Replicating Functions
Documentation Home -> Gameplay Guide -> Testing Multiplayer
Documentation Home -> Programming Guide -> Unreal Architecture -> UFunctions -> Function Specifiers
Documentation Home -> Samples and Tutorials -> Content Examples -> Networking Content Examples
Networking/Replication
Network Guide
A Crash Course in Blueprint