0


UE5——网络——属性复制

当属性被注册进行复制后,您将无法再取消注册(涉及到生存期这一话题)。之所以会这样,是因为我们要预制尽可能多的信息,以便针对同一组属性将某一工作分担给多个连接。这样可以节省大量的计算时间。

virtualvoidGetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps)constoverride;
voidARPCProjectCharacter::GetLifetimeReplicatedProps(TArray <FLifetimeProperty>& OutLifetimeProps)const{Super::GetLifetimeReplicatedProps(OutLifetimeProps);DOREPLIFETIME_CONDITION(ARPCProjectCharacter, OverlappingWeapon, COND_OwnerOnly);DOREPLIFETIME(ARPCProjectCharacter, Health);DOREPLIFETIME_ACTIVE_OVERRIDE( ARPCProjectCharacter, ReplicatedMovement, bReplicateMovement );}
/** Secondary condition to check before considering the replication of a lifetime property. */UENUM(BlueprintType)enumELifetimeCondition{
    COND_None =0UMETA(DisplayName ="None"),// This property has no condition, and will send anytime it changes
    COND_InitialOnly =1UMETA(DisplayName ="Initial Only"),// This property will only attempt to send on the initial bunch
    COND_OwnerOnly =2UMETA(DisplayName ="Owner Only"),// This property will only send to the actor's owner
    COND_SkipOwner =3UMETA(DisplayName ="Skip Owner"),// This property send to every connection EXCEPT the owner
    COND_SimulatedOnly =4UMETA(DisplayName ="Simulated Only"),// This property will only send to simulated actors
    COND_AutonomousOnly =5UMETA(DisplayName ="Autonomous Only"),// This property will only send to autonomous actors
    COND_SimulatedOrPhysics =6UMETA(DisplayName ="Simulated Or Physics"),// This property will send to simulated OR bRepPhysics actors
    COND_InitialOrOwner =7UMETA(DisplayName ="Initial Or Owner"),// This property will send on the initial packet, or to the actors owner
    COND_Custom =8UMETA(DisplayName ="Custom"),// This property has no particular condition, but wants the ability to toggle on/off via SetCustomIsActiveOverride
    COND_ReplayOrOwner =9UMETA(DisplayName ="Replay Or Owner"),// This property will only send to the replay connection, or to the actors owner
    COND_ReplayOnly =10UMETA(DisplayName ="Replay Only"),// This property will only send to the replay connection
    COND_SimulatedOnlyNoReplay =11UMETA(DisplayName ="Simulated Only No Replay"),// This property will send to actors only, but not to replay connections
    COND_SimulatedOrPhysicsNoReplay =12UMETA(DisplayName ="Simulated Or Physics No Replay"),// This property will send to simulated Or bRepPhysics actors, but not to replay connections
    COND_SkipReplay =13UMETA(DisplayName ="Skip Replay"),// This property will not send to the replay connection
    COND_Never =15UMETA(Hidden),// This property will never be replicated
    COND_Max =16UMETA(Hidden)};

COND_InitialOnly :此属性将仅尝试在初始话的时候发送
COND_OwnerOnly:此属性仅发送至 actor 的所有者
COND_SkipOwner:该属性将发送至除所有者之外的每个连接
COND_SimulatedOnly:该属性仅发送至模拟 actor
COND_AutonomousOnly:该属性仅发送给自治 actor
COND_SimulatedOrPhysics :该属性将发送至模拟或 bRepPhysics actor
COND_InitialOrOwner :该属性将发送初始数据包,或者发送至 actor 所有者
COND_Custom :该属性没有特定条件,但需要通过 SetCustomIsActiveOverride 得到开启/关闭能力
COND_ReplayOrOwner :此属性将仅发送到重播连接,或 actor所有者
COND_ReplayOnly :此属性将只发送到重播连接
COND_SimulatedOnlyNoReplay :此属性将仅发送给参与者,而不发送给重播连接
COND_SimulatedOrPhysicsNoReplay:此属性将发送给模拟的Or bRepPhysics参与者,但不发送给重播连接
COND_Never :此属性不会发送到重播连接

DOREPLIFETIME_ACTIVE_OVERRIDE 的宏可以让您进行全面控制,利用您想要的任何定制条件来决定何时复制/不复制某个属性。需要注意的是,这种控制需针对每个 actor(而不是每条连接)逐一进行。换句话说,如果在定制条件中使用一个可根据连接而发生变化的状态,会存在一定的安全风险。
现在 ReplicatedMovement 属性只会在 bReplicateMovement 为 true 时复制。

为何不一直使用这个宏?主要有两个原因:

1.如果定制条件的值变化太大,这种做法会降低执行速度。

2.您不能使用根据连接而变化的条件(此时不检查 RemoteRole)

Replicated

voidARPCProjectCharacter::GetLifetimeReplicatedProps(TArray <FLifetimeProperty>& OutLifetimeProps)const{Super::GetLifetimeReplicatedProps(OutLifetimeProps);//复制当前生命值。DOREPLIFETIME(ARPCProjectCharacter, HP);}
voidARPCProjectCharacter::StartFire(){FunctionServer();}
UFUNCTION(Server, Reliable)voidFunctionServer();
voidARPCProjectCharacter::FunctionServer_Implementation(){

    HP-=10;}
voidARPCProjectCharacter::Tick(float DeltaSeconds){Super::Tick(DeltaSeconds);if(HasAuthority()){UE_LOG(LogTemp,Warning,TEXT("HPTick is: %f "),HP);}else{
        GEngine->AddOnScreenDebugMessage(-1,1.f, FColor::Red,FString::Printf(TEXT("HPTickHello world, this is %f"), HP));}}

在这里插入图片描述

ReplicatedUsing

UFUNCTION(Server, Reliable)voidFunctionServer();UPROPERTY(ReplicatedUsing=OnRep_WhatOn)float WhatOn=100;/** RepNotify,用于同步对当前生命值所做的更改。*/UFUNCTION()voidOnRep_WhatOn();
voidARPCProjectCharacter::GetLifetimeReplicatedProps(TArray <FLifetimeProperty>& OutLifetimeProps)const{Super::GetLifetimeReplicatedProps(OutLifetimeProps);//复制当前生命值。DOREPLIFETIME(ARPCProjectCharacter, WhatOn);}
voidARPCProjectCharacter::StartFire(){FunctionServer();}
voidARPCProjectCharacter::FunctionServer_Implementation(){
    WhatOn-=10;}
voidARPCProjectCharacter::OnRep_WhatOn(){
    GEngine->AddOnScreenDebugMessage(-1,5.f, FColor::Red,FString::Printf(TEXT("Health, this is %f"), WhatOn));UE_LOG(LogTemp,Warning,TEXT("Health is: %f "),WhatOn);}

在这里插入图片描述

标签: 网络 ue5 前端

本文转载自: https://blog.csdn.net/weixin_43945471/article/details/134163230
版权归原作者 吴俊荣 所有, 如有侵权,请联系我们删除。

“UE5——网络——属性复制”的评论:

还没有评论