UE4 c ++変数RepNotify
Ue4 C Variable Repnotify
焦点は主に3つのステップにあります
1.関数をオーバーロードし、beginPlay関数でbReplicates = trueを設定します
virtual void GetLifetimeReplicatedProps(TArray&OutLifetimeProps)constオーバーライド
2.変数を宣言し、関数に通知します
UPROPERTY(VisibleAnywhere, ReplicatedUsing= OnRep_CurrentLife, BlueprintReadWrite) float CurrentLife UFUNCTION() void OnRep_CurrentLife()
3.同期変数を設定します
void AMyPlayerState::GetLifetimeReplicatedProps(TArray& OutLifetimeProps)const { Super::GetLifetimeReplicatedProps(OutLifetimeProps) DOREPLIFETIME(AMyPlayerState, TotalLife) DOREPLIFETIME(AMyPlayerState, CurrentLife) }
最後に、完全なPlayerState同期変数を提供します
.Hファイル
// Fill out your copyright notice in the Description page of Project Settings. #pragma once #include 'CoreMinimal.h' #include 'GameFramework/PlayerState.h' #include 'MyPlayerState.generated.h' /** * */ UCLASS() class TESTFORCPLUSREP_API AMyPlayerState : public APlayerState { GENERATED_BODY() public: AMyPlayerState() virtual void BeginPlay() override virtual void GetLifetimeReplicatedProps(TArray& OutLifetimeProps)const UFUNCTION(Server, Unreliable, WithValidation, BlueprintCallable) void ApplyDamage(float damage) bool ApplyDamage_Validate(float damage) void ApplyDamage_Implementation(float damage) public: virtual void Tick(float DeltaSeconds)override UPROPERTY(VisibleAnywhere, Replicated, BlueprintReadWrite) float TotalLife UPROPERTY(VisibleAnywhere, ReplicatedUsing= OnRep_CurrentLife, BlueprintReadWrite) float CurrentLife UFUNCTION() void OnRep_CurrentLife() }
.CPPファイル
// Fill out your copyright notice in the Description page of Project Settings. #include 'MyPlayerState.h' #include 'UnrealNetwork.h' AMyPlayerState::AMyPlayerState() { PrimaryActorTick.bCanEverTick = true TotalLife = 100.0f CurrentLife = 100.0f } void AMyPlayerState::BeginPlay() { Super::BeginPlay() bReplicates = true } void AMyPlayerState::GetLifetimeReplicatedProps(TArray& OutLifetimeProps)const { Super::GetLifetimeReplicatedProps(OutLifetimeProps) DOREPLIFETIME(AMyPlayerState, TotalLife) DOREPLIFETIME(AMyPlayerState, CurrentLife) } // Called every frame void AMyPlayerState::Tick(float DeltaTime) { Super::Tick(DeltaTime) } void AMyPlayerState::OnRep_CurrentLife() { UE_LOG(LogTemp, Warning, TEXT('OnRep_CruuentLife')) } bool AMyPlayerState::ApplyDamage_Validate(float damage) { return true } void AMyPlayerState::ApplyDamage_Implementation(float damage) { UE_LOG(LogTemp, Warning, TEXT('ApplyDamage')) if (damage > CurrentLife) damage = CurrentLife CurrentLife -= damage OnRep_CurrentLife() }