.h
UPROPERTY(Replicated,VisibleAnywhere)
float HP =100.f;
UPROPERTY(ReplicatedUsing=OnRep_Armor)
float armor = 0.f;
UFUNCTION()
void OnRep_Armor();
.cpp
#include "Net/UnrealNetwork.h"
AMyActor::AMyActor()
{
bReplicates = true;.
}
void AMyActor::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
if (HasAuthority())
{
UE_LOG(LogTemp, Warning, TEXT("Server Health :%f"), HP);
armor = FMath::Rand();
}
else {
UE_LOG(LogTemp, Warning, TEXT("Client Health :%f"), HP);
}
}
void AMyActor::OnRep_Armor()
{
if (HasAuthority())
{
UE_LOG(LogTemp, Warning, TEXT("Server Armor :%f"), armor);
}
else {
UE_LOG(LogTemp, Warning, TEXT("Client Armor :%f"), armor);
}
}
void AMyActor::GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const
{
Super::GetLifetimeReplicatedProps(OutLifetimeProps);
DOREPLIFETIME(AMyActor, HP);
DOREPLIFETIME(AMyActor, armor);
}
|