当前位置:   article > 正文

Unreal Engine 4:学习笔记(十七)Networking & Multiplayer_如何设置actorcomponent的复制属性

如何设置actorcomponent的复制属性

目录

一、网络模式类型

二、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会同时起作用。

  1. UPROPERTY(Replicated)
  2. float Health;
  3. #include "UnrealNetwork.h"
  4. // ...
  5. void AYourCharacter::GetLifetimeReplicatedProps(TArray< FLifetimeProperty > & OutLifetimeProps) const
  6. {
  7. Super::GetLifetimeReplicatedProps(OutLifetimeProps);
  8. DOREPLIFETIME(AYourCharacter, Health);
  9. }

  1. UPROPERTY( ReplicatedUsing=OnRep_Health )
  2. float Health;
  3. UFUNCTION( )
  4. void OnRep_Health( );
  5. void MyCharacter::GetLifetimeReplicatedProps( TArray< FLifetimeProperty > & OutLifetimeProps ) const
  6. {
  7. Super::GetLifetimeReplicatedProps( OutLifetimeProps );
  8. DOREPLIFETIME( MyCharacter, Health);
  9. }
  10. void MyCharacter::OnRep_Health( )
  11. {
  12. // Handle health changed
  13. }

2.Actor&Component复制

C++:

  1. AActorComponent::SetIsReplicated(true)
  2. AActor::SetReplicates(true

Blueprint:
属性面板中设置Replicates,或调用SetIsReplicated节点

3.RPC

  1. // server to a client
  2. UFUNCTION( Client )
  3. void Function(...)
  4. void Function_Implementation(...)
  5. // a client to server
  6. UFUNCTION( Server )
  7. void Function(...)
  8. void Function_Implementation(...)
  9. void Function_Validate(...)
  10. // server to clients
  11. UFUNCTION( NetMulticast )
  12. void Function(...)
  13. 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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/91419
推荐阅读
相关标签
  

闽ICP备14008679号