ue4-Network相关-变量同步

蓝图 or c++ 的 变量同步


变量同步

  • 变量同步的原则是:一定要在服务端修改该变量,也就是rpc调用 Run On Server方法(ue4-Network相关-rpc调用)中修改该变量,才会根据变量属性是否为 Replicated 决定是否同步给 服务器和所有客户端
  • 蓝图中同步:

  • c++中同步:

    1. 在变量中加入标记 Replicated

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

      1
      2
      3
      4
      5
      6
      7
      #include "Net/UnrealNetwork.h" //包含net相关头文件

      void AMyNetCharacter::GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const
      {
      Super::GetLifetimeReplicatedProps(OutLifetimeProps);
      DOREPLIFETIME(AMyNetCharacter, Cash);
      }
  1. 在构造函数中修改成员 bReplicatestrue

    1
    2
    3
    4
    5
    AMyNetCharacter::AMyNetCharacter()
    {
    Cash = 456;
    bReplicates = true;
    }
    • 变量标记的可选值,和蓝图对应,意思差不多

      • NotReplicated:服务端不会同步给所有客户端
      • Replicated:在服务端修改,会同步给其他客户端
      • ReplicatedUsing:选择这个的时候,需要指定一个 Var 改变时对应的 OnRep_Var(命名无特殊要求,不过还是按照蓝图生成的规则以 OnRep_ 前缀),一定要加上 UFUNCTION 给反射系统,在服务端修改 Var 的时候会回调这个 OnRep_Var 方法。规则和 Replicated 差不多,示例如下:
        1
        2
        3
        4
        5
        6
        7
        8
        9
        10
        11
        12
        13
        14
        15
        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

    1
    2
    3
    4
    5
    6
    7
    8
    9
    	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 标记。
    这里写图片描述

  • c++中的控制条件枚举

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    /** 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