From 84b58df6c94c343abda4a2bb515bbe6f4c394309 Mon Sep 17 00:00:00 2001
From: David Gilbert <gilbert@vr.rwth-aachen.de>
Date: Fri, 9 Aug 2024 13:46:57 +0200
Subject: [PATCH] fix(replication, cave): Fixes nullptr access when logging

---
 .../Private/Pawn/ClusterRepresentationActor.cpp          | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)

diff --git a/Source/RWTHVRToolkit/Private/Pawn/ClusterRepresentationActor.cpp b/Source/RWTHVRToolkit/Private/Pawn/ClusterRepresentationActor.cpp
index d182cbe6..bd4a4000 100644
--- a/Source/RWTHVRToolkit/Private/Pawn/ClusterRepresentationActor.cpp
+++ b/Source/RWTHVRToolkit/Private/Pawn/ClusterRepresentationActor.cpp
@@ -79,12 +79,17 @@ void AClusterRepresentationActor::AttachDCRAIfRequired(const ARWTHVRPlayerState*
 		return;
 	}
 
+	const auto CCA = PlayerState->GetCorrespondingClusterActor();
+	
+	if (CCA == nullptr) // this can happen often if property isn't replicated yet, this is okay.
+		return;
+	
 	UE_LOGFMT(Toolkit, Display,
 			  "{Name} AttachDCRAIfRequired: Player State is {PlayerState}, PlayerState->CCA is {CCA}.", GetName(),
-			  PlayerState->GetName(), PlayerState->GetCorrespondingClusterActor()->GetName());
+			  PlayerState->GetName(), CCA->GetName());
 
 	// The local player this is executed on corresponds to this actor
-	if (PlayerState->GetCorrespondingClusterActor() == this)
+	if (CCA == this)
 	{
 		UE_LOGFMT(Toolkit, Display, "{Name} AttachDCRAIfRequired: Attaching DCRA to {Name}.", GetName());
 
-- 
GitLab