ue4-Network相关-变量同步
2017-01-28 15:03
1306 查看
蓝图 or c++ 的 变量同步
蓝图中同步:
选择变量属性 Replication 为 Replicated,有三个可选值
None:默认值,服务端不会同步给所有客户端
Replicated:在服务端修改,会同步给其他客户端
RepNotify:选择这个的时候,会 Functions 中生产一个变量 Var 对应的方法 OnRep_Var,在服务器修改 Var 的时候会回调 服务器和所有客户端 中该Actor的这个 OnRep_Var 方法,
参考:https://docs-origin.unrealengine.com/latest/INT/Gameplay/HowTo/Networking/ReplicateVariable/Blueprints/index.html
c++中同步:
在变量中加入标记 Replicated
重写 AActor 的 GetLifetimeReplicatedProps 方法,加入需要同步的变量 Cash
在构造函数中修改成员 bReplicates 为 true
变量标记的可选值,和蓝图对应,意思差不多
NotReplicated:服务端不会同步给所有客户端
Replicated:在服务端修改,会同步给其他客户端
ReplicatedUsing:选择这个的时候,需要指定一个 Var 改变时对应的 OnRep_Var(命名无特殊要求,不过还是按照蓝图生成的规则以 OnRep_ 前缀),一定要加上 UFUNCTION 给反射系统,在服务端修改 Var 的时候会回调这个 OnRep_Var 方法。规则和 Replicated 差不多,示例如下:
参考:https://docs-origin.unrealengine.com/latest/INT/Gameplay/Networking/Actors/Properties/index.html
实际的作用场景,例如:本机控制的actor的一些属性,本地修改(假修改),同时告诉服务端修改(真实修改,同时做校验),然后同步给除本机外的所有客户端上的该actor修改,因为本机已经自己修改了。
做法和上面差不多,只需要修改第二步, DOREPLIFETIME_CONDITION
然后在rpc调用服务端方法修改Item1变量,就能看到只有自己控制的actor中Item1改变了,其他客户端的没有改变,是因为这个 COND_AutonomousOnly 标记。
CallServerChangeItem1 是个 Run On Server 类型的方法。不明白的看这里:ue4-Network相关-rpc调用
c++中的控制条件枚举
蓝图中则有个 Replication Condition 下拉选项:
参考:https://docs-origin.unrealengine.com/latest/INT/Gameplay/Networking/Actors/Properties/Conditions/index.html
变量同步
变量同步的原则是:一定要在服务端修改该变量,也就是rpc调用 Run On Server方法(ue4-Network相关-rpc调用)中修改该变量,才会根据变量属性是否为 Replicated 决定是否同步给 服务器和所有客户端蓝图中同步:
选择变量属性 Replication 为 Replicated,有三个可选值
None:默认值,服务端不会同步给所有客户端
Replicated:在服务端修改,会同步给其他客户端
RepNotify:选择这个的时候,会 Functions 中生产一个变量 Var 对应的方法 OnRep_Var,在服务器修改 Var 的时候会回调 服务器和所有客户端 中该Actor的这个 OnRep_Var 方法,
参考:https://docs-origin.unrealengine.com/latest/INT/Gameplay/HowTo/Networking/ReplicateVariable/Blueprints/index.html
c++中同步:
在变量中加入标记 Replicated
UPROPERTY(Replicated, EditAnywhere, BlueprintReadWrite, Category = AMyNetCharacter) int32 Cash;
重写 AActor 的 GetLifetimeReplicatedProps 方法,加入需要同步的变量 Cash
#include "Net/UnrealNetwork.h" //包含net相关头文件 void AMyNetCharacter::GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const { Super::GetLifetimeReplicatedProps(OutLifetimeProps); DOREPLIFETIME(AMyNetCharacter, Cash); }
在构造函数中修改成员 bReplicates 为 true
AMyNetCharacter::AMyNetCharacter() { Cash = 456; bReplicates = true; }
变量标记的可选值,和蓝图对应,意思差不多
NotReplicated:服务端不会同步给所有客户端
Replicated:在服务端修改,会同步给其他客户端
ReplicatedUsing:选择这个的时候,需要指定一个 Var 改变时对应的 OnRep_Var(命名无特殊要求,不过还是按照蓝图生成的规则以 OnRep_ 前缀),一定要加上 UFUNCTION 给反射系统,在服务端修改 Var 的时候会回调这个 OnRep_Var 方法。规则和 Replicated 差不多,示例如下:
UFUNCTION(BlueprintCallable, Category = AMyNetCharacter) virtual void OnRep_Money(); void AMyNetCharacter::OnRep_Money() { FString msg = FString::Printf(TEXT("--- OnRep_Money money:%d"), Money); GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, *msg); } // 指定回调函数为 OnRep_Money UPROPERTY(ReplicatedUsing = OnRep_Money, EditAnywhere, BlueprintReadWrite, Category = AMyNetCharacter) int32 Money; //重写的 GetLifetimeReplicatedProps 方法中加入需要同步的变量 DOREPLIFETIME(AMyNetCharacter, Money);
参考:https://docs-origin.unrealengine.com/latest/INT/Gameplay/Networking/Actors/Properties/index.html
有条件的同步给不同链接中的actor的变量
会在服务端同步 Replicated 变量给所有客户端前做二次检查,满足条件的客户端才会被同步实际的作用场景,例如:本机控制的actor的一些属性,本地修改(假修改),同时告诉服务端修改(真实修改,同时做校验),然后同步给除本机外的所有客户端上的该actor修改,因为本机已经自己修改了。
做法和上面差不多,只需要修改第二步, DOREPLIFETIME_CONDITION
UPROPERTY(Replicated, EditAnywhere, BlueprintReadWrite, Category = AMyNetCharacter) int32 Item1; void AMyNetCharacter::GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const { Super::GetLifetimeReplicatedProps(OutLifetimeProps); DOREPLIFETIME_CONDITION(AMyNetCharacter, Item1, COND_AutonomousOnly); //只给自己控制的actor同步Item1变量 }
然后在rpc调用服务端方法修改Item1变量,就能看到只有自己控制的actor中Item1改变了,其他客户端的没有改变,是因为这个 COND_AutonomousOnly 标记。
CallServerChangeItem1 是个 Run On Server 类型的方法。不明白的看这里:ue4-Network相关-rpc调用
c++中的控制条件枚举
/** Secondary condition to check before considering the replication of a lifetime property. */ UENUM(BlueprintType) enum ELifetimeCondition { COND_None = 0 UMETA(DisplayName = "None"), // This property has no condition, and will send anytime it changes COND_InitialOnly = 1 UMETA(DisplayName = "Initial Only"), // This property will only attempt to send on the initial bunch COND_OwnerOnly = 2 UMETA(DisplayName = "Owner Only"), // This property will only send to the actor's owner COND_SkipOwner = 3 UMETA(DisplayName = "Skip Owner"), // This property send to every connection EXCEPT the owner COND_SimulatedOnly = 4 UMETA(DisplayName = "Simulated Only"), // This property will only send to simulated actors COND_AutonomousOnly = 5 UMETA(DisplayName = "Autonomous Only"), // This property will only send to autonomous actors COND_SimulatedOrPhysics = 6 UMETA(DisplayName = "Simulated Or Physics"), // This property will send to simulated OR bRepPhysics actors COND_InitialOrOwner = 7 UMETA(DisplayName = "Initial Or Owner"), // This property will send on the initial packet, or to the actors owner COND_Custom = 8 UMETA(DisplayName = "Custom"), // This property has no particular condition, but wants the ability to toggle on/off via SetCustomIsActiveOverride COND_ReplayOrOwner = 9 UMETA(DisplayName = "Replay Or Owner"), // This property will only send to the replay connection, or to the actors owner COND_ReplayOnly = 10 UMETA(DisplayName = "Replay Only"), // This property will only send to the replay connection COND_SimulatedOnlyNoReplay = 11 UMETA(DisplayName = "Simulated Only No Replay"), // This property will send to actors only, but not to replay connections COND_SimulatedOrPhysicsNoReplay = 12 UMETA(DisplayName = "Simulated Or Physics No Replay"), // This property will send to simulated Or bRepPhysics actors, but not to replay connections COND_Max = 13 UMETA(Hidden) };
蓝图中则有个 Replication Condition 下拉选项:
参考:https://docs-origin.unrealengine.com/latest/INT/Gameplay/Networking/Actors/Properties/Conditions/index.html
相关文章推荐
- ue4-Network相关-组件及数据同步
- ue4-Network相关-Client和Server的区分
- ue4-Network相关-总览
- ue4-Network相关-rpc调用
- Java多线程初学者指南(12):使用Synchronized块同步变量
- PHP - Manual手册 - Function Reference函数参考 - Variable and Type Related Extensions变量和类型相关扩展 - Variable handling变量操作 - Variable handl
- 使用InterlockedExchangeAdd同步全局变量
- 多线程中对全局变量(整型类型)是否需要同步操作
- 再读内核存储管理(1):相关的全局变量
- 有的网站的相关内容必须要在登录后才可以查看,其登录信息保存在session变量之中。这样,使用asphttp等组件就难以正确得到所要的信息。
- gcc相关的环境变量
- Axure RP Pro - 相关问题 - 使用masters组件监视变量
- Delphi中网络实现线程变量同步
- Mysql:命令选项、配置选项、(全局、会话)系统变量、状态变量:和路径、文件名称相关的设置
- 音视频信号同步的相关问题
- Axure RP Pro - 相关问题 - 设定多套初始化变量
- uclinux-2008r1(bf561)内核中与存储管理相关的几个全局变量
- Axure RP Pro - 相关问题 - 监视变量
- C++中有关volatile关键字的作用--阻止编译器将其变量优化缓存到寄存器(和线程相关)(转自百度)