当属性被注册进行复制后,您将无法再取消注册(涉及到生存期这一话题)。之所以会这样,是因为我们要预制尽可能多的信息,以便针对同一组属性将某一工作分担给多个连接。这样可以节省大量的计算时间。
virtual void GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const override;
void ARPCProjectCharacter::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)
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_SkipReplay = 13 UMETA(DisplayName = "Skip Replay"), // This property will not send to the replay connection
COND_Never = 15 UMETA(Hidden), // This property will never be replicated
COND_Max = 16 UMETA(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
void ARPCProjectCharacter::GetLifetimeReplicatedProps(TArray <FLifetimeProperty> & OutLifetimeProps) const
{
Super::GetLifetimeReplicatedProps(OutLifetimeProps);
//复制当前生命值。
DOREPLIFETIME(ARPCProjectCharacter, HP);
}
void ARPCProjectCharacter::StartFire()
{
FunctionServer();
}
UFUNCTION(Server, Reliable)
void FunctionServer();
void ARPCProjectCharacter::FunctionServer_Implementation()
{
HP-=10;
}
void ARPCProjectCharacter::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)
void FunctionServer();
UPROPERTY(ReplicatedUsing=OnRep_WhatOn)
float WhatOn=100;
/** RepNotify,用于同步对当前生命值所做的更改。*/
UFUNCTION()
void OnRep_WhatOn();
void ARPCProjectCharacter::GetLifetimeReplicatedProps(TArray <FLifetimeProperty> & OutLifetimeProps) const
{
Super::GetLifetimeReplicatedProps(OutLifetimeProps);
//复制当前生命值。
DOREPLIFETIME(ARPCProjectCharacter, WhatOn);
}
void ARPCProjectCharacter::StartFire()
{
FunctionServer();
}
void ARPCProjectCharacter::FunctionServer_Implementation()
{
WhatOn-=10;
}
void ARPCProjectCharacter::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);
}