Unreal Engine 4:学习笔记(十七)Networking & Multiplayer

目录

一、网络模式类型

二、Client-Server模式下的使用方式

三、数据复制方式


一、网络模式类型

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

UE4Editor.exe ProjectName MapName?Listen -game

Dedicated Server

UE4Editor.exe ProjectName MapName -server -game -log -port=ServerIP

Client

UE4Editor.exe ProjectName ServerIP -game

2.客户端接入后会依次调用如下方法
AGameModeBase::PreLogin        可以在此判断是否要拒绝客户端连接
AGameModeBase::Login              会创建PlayerController,并执行APlayerController::BeginPlay
AGameModeBase::PostLogin       在此之后可以调用PlayerController上的RPC函数

3.客户端和服务端的角色

on server:
    Role == ROLE_Authority     // server
    RemoteRole == ROLE_SimulatedProxy/ROLE_AutonomousProxy    // client

on 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

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值