From 16b89cf6be0090c2853792fe3aa72db85c707cb6 Mon Sep 17 00:00:00 2001
From: David Gilbert <gilbert@vr.rwth-aachen.de>
Date: Fri, 9 Aug 2024 13:13:46 +0200
Subject: [PATCH] fix(replication, cave): Only executes DCRA Attachment on_rep
 of owned player state. Adds some logs to see if it works.

---
 .../Pawn/ClusterRepresentationActor.cpp       | 26 +++++++++++--------
 .../Public/Core/RWTHVRPlayerState.h           |  7 ++++-
 2 files changed, 21 insertions(+), 12 deletions(-)

diff --git a/Source/RWTHVRToolkit/Private/Pawn/ClusterRepresentationActor.cpp b/Source/RWTHVRToolkit/Private/Pawn/ClusterRepresentationActor.cpp
index 59dc1f4e..d182cbe6 100644
--- a/Source/RWTHVRToolkit/Private/Pawn/ClusterRepresentationActor.cpp
+++ b/Source/RWTHVRToolkit/Private/Pawn/ClusterRepresentationActor.cpp
@@ -50,12 +50,12 @@ void AClusterRepresentationActor::AttachDCRAIfRequired(const ARWTHVRPlayerState*
 
 	if (bIsAttached)
 	{
-		UE_LOGFMT(Toolkit, Display,
-				  "AClusterRepresentationActor::AttachDCRAIfRequired: Already attached, skipping repeated attachment.");
+		UE_LOGFMT(Toolkit, Display, "{Name} AttachDCRAIfRequired: Already attached, skipping repeated attachment.",
+				  *this->GetName());
 		return;
 	}
 
-	UE_LOGFMT(Toolkit, Display, "AClusterRepresentationActor::AttachDCRAIfRequired: Starting DCRA Attachment process.");
+	UE_LOGFMT(Toolkit, Display, "{Name} AttachDCRAIfRequired: Starting DCRA Attachment process.", *this->GetName());
 
 	// This should give us the first local player controller
 	const auto* PlayerController = UGameplayStatics::GetPlayerController(GetWorld(), 0);
@@ -63,9 +63,9 @@ void AClusterRepresentationActor::AttachDCRAIfRequired(const ARWTHVRPlayerState*
 	// Only run this for the local controller - redundant, but double check
 	if (!PlayerController || !PlayerController->IsLocalController())
 	{
-		UE_LOGFMT(
-			Toolkit, Warning,
-			"AClusterRepresentationActor::AttachDCRAIfRequired: PlayerController not valid or not locally controlled.");
+		UE_LOGFMT(Toolkit, Warning,
+				  "{Name} AttachDCRAIfRequired: PlayerController not valid or not locally controlled.",
+				  *this->GetName());
 		return;
 	}
 	const auto* PlayerState =
@@ -73,16 +73,20 @@ void AClusterRepresentationActor::AttachDCRAIfRequired(const ARWTHVRPlayerState*
 	if (!PlayerState)
 	{
 		UE_LOGFMT(Toolkit, Error,
-				  "AClusterRepresentationActor::AttachDCRAIfRequired: PlayerState is not valid or not of type "
-				  "ARWTHVRPlayerState.");
+				  "{Name} AttachDCRAIfRequired: PlayerState is not valid or not of type "
+				  "ARWTHVRPlayerState.",
+				  *this->GetName());
 		return;
 	}
 
+	UE_LOGFMT(Toolkit, Display,
+			  "{Name} AttachDCRAIfRequired: Player State is {PlayerState}, PlayerState->CCA is {CCA}.", GetName(),
+			  PlayerState->GetName(), PlayerState->GetCorrespondingClusterActor()->GetName());
+
 	// The local player this is executed on corresponds to this actor
 	if (PlayerState->GetCorrespondingClusterActor() == this)
 	{
-		UE_LOGFMT(Toolkit, Display, "AClusterRepresentationActor::AttachDCRAIfRequired: Attaching DCRA to {Name}.",
-				  GetName());
+		UE_LOGFMT(Toolkit, Display, "{Name} AttachDCRAIfRequired: Attaching DCRA to {Name}.", GetName());
 
 		bIsAttached = AttachDCRA();
 	}
@@ -119,7 +123,7 @@ bool AClusterRepresentationActor::AttachDCRA()
 		}
 
 		bool bAttached = DCRA->AttachToActor(this, FAttachmentTransformRules::SnapToTargetNotIncludingScale);
-		UE_LOGFMT(Toolkit, Display, "Attaching {This} to DCRA {DCRA} returned {Res}", GetName(), DCRA->GetName(),
+		UE_LOGFMT(Toolkit, Display, "Attaching DCRA {DCRA} to {this} returned {Res}", GetName(), DCRA->GetName(),
 				  bAttached);
 
 		DCRA->SetActorEnableCollision(false);
diff --git a/Source/RWTHVRToolkit/Public/Core/RWTHVRPlayerState.h b/Source/RWTHVRToolkit/Public/Core/RWTHVRPlayerState.h
index 5c353027..040c251a 100644
--- a/Source/RWTHVRToolkit/Public/Core/RWTHVRPlayerState.h
+++ b/Source/RWTHVRToolkit/Public/Core/RWTHVRPlayerState.h
@@ -40,7 +40,12 @@ private:
 	TObjectPtr<AClusterRepresentationActor> CorrespondingClusterActor;
 
 	UFUNCTION()
-	virtual void OnRep_CorrespondingClusterActor() { CorrespondingClusterActor->AttachDCRAIfRequired(this); }
+	virtual void OnRep_CorrespondingClusterActor()
+	{
+		// Only execute this on the local owning player
+		if (HasLocalNetOwner())
+			CorrespondingClusterActor->AttachDCRAIfRequired(this);
+	}
 
 	UFUNCTION(Reliable, Server)
 	void ServerSetPlayerTypeRpc(EPlayerType NewPlayerType);
-- 
GitLab