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++中同步:

    1. 在变量中加入标记 Replicated

          UPROPERTY(Replicated, EditAnywhere, BlueprintReadWrite, Category = AMyNetCharacter)        int32 Cash;
    2. 重写 AActor 的 GetLifetimeReplicatedProps 方法,加入需要同步的变量 Cash

      #include "Net/UnrealNetwork.h" //包含net相关头文件void AMyNetCharacter::GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const{    Super::GetLifetimeReplicatedProps(OutLifetimeProps);    DOREPLIFETIME(AMyNetCharacter, Cash);}
    3. 在构造函数中修改成员 bReplicatestrue

      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修改,因为本机已经自己修改了。
  1. 做法和上面差不多,只需要修改第二步, 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变量}
  2. 然后在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

0 0
原创粉丝点击