ue4-Network相关-变量同步
来源:互联网 发布:大连贵金属行情软件 编辑:程序博客网 时间:2024/06/04 19:38
蓝图 or c++ 的 变量同步
变量同步
- 变量同步的原则是:一定要在服务端修改该变量,也就是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相关-组件及数据同步
- ue4-Network相关-总览
- ue4-Network相关-rpc调用
- ue4-Network相关-Client和Server的区分
- UE4的NetWork原理
- 多线程同步的条件变量相关函数
- UE4的NetWork简单原理
- UE4的NetWork简单原理
- UE4 服务器同步
- network相关
- 2.UE4的变量
- UE4 Login相关代码
- UE4-蓝图基础:变量类型
- unity network 数据同步
- ue4 2游戏构架相关
- [UE4]Component相关常用API
- ue4 动画相关方法杂记
- 数组下标越界解析(以C语言为例)
- Andriod ADB Interface驱动安装失败Configure USB Debug for Android
- 按位与、或、异或等运算方法
- Codevs 2292 图灵机游戏 解题报告
- AndroidStudio入门笔记
- ue4-Network相关-变量同步
- hdu 4704 sum 大整数取模+费马小定理+数快速幂
- ue4-Network相关-rpc调用
- ue4-Network相关-Client和Server的区分
- Luogu P1216 数字金字塔+动态规划
- Android内存泄露---检测工具篇
- 1096.Consecutive Factors (20)
- 伸展树,LCP(uva,11996)
- Android内存泄露---检测工具篇_0