From 4ce616c63e56f8f6cff70ffe281e6b730c8bac29 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Malte=20K=C3=B6gel?= <koegel@vr.rwth-aachen.de>
Date: Mon, 11 Sep 2023 15:02:07 +0200
Subject: [PATCH] WIP seems to work. Needs some final testing in eyetracking
 test map, then removal of debugging functions

---
 .../Private/GazeTracking/SFGazeTracker.cpp      | 17 ++++++++++-------
 1 file changed, 10 insertions(+), 7 deletions(-)

diff --git a/Source/StudyFrameworkPlugin/Private/GazeTracking/SFGazeTracker.cpp b/Source/StudyFrameworkPlugin/Private/GazeTracking/SFGazeTracker.cpp
index 702d7e8..d7cf39f 100644
--- a/Source/StudyFrameworkPlugin/Private/GazeTracking/SFGazeTracker.cpp
+++ b/Source/StudyFrameworkPlugin/Private/GazeTracking/SFGazeTracker.cpp
@@ -27,16 +27,19 @@ bool USFGazeTracker::Tick(float DeltaTime)
 #ifdef WITH_SRANIPAL
 		bIsAsyncEyeTrackingTaskRunning = true;
 		ViveSR::anipal::Eye::EyeData_v2 TempEyeData;
-		AsyncTask(ENamedThreads::AnyThread, [TempEyeDataAddr = &TempEyeData, SranipalEyeData = MoveTemp(SranipalEyeData), bIsAsyncEyeTrackingTaskRunning = MoveTemp(bIsAsyncEyeTrackingTaskRunning), bDataLogged = MoveTemp(bDataLogged)]()
-			{				
-				int Result = ViveSR::anipal::Eye::GetEyeData_v2(TempEyeDataAddr);
-
-				AsyncTask(ENamedThreads::GameThread, [TempEyeDataAddr =TempEyeDataAddr, SranipalEyeData, bIsAsyncEyeTrackingTaskRunning, bDataLogged, Result]() mutable
+		//transfer 'this' into the function in order to have access to the boolean member variables in the nested GameThread-async task
+		AsyncTask(ENamedThreads::AnyThread, [this]()
+			{
+				ViveSR::anipal::Eye::EyeData_v2 LocalEyeData;
+				int Result = ViveSR::anipal::Eye::GetEyeData_v2(&LocalEyeData);
+				// Set member variables from GameThread to avoid race conditions
+				AsyncTask(ENamedThreads::GameThread, [this, LocalEyeData =MoveTemp(LocalEyeData), Result]() mutable
 					{
-						SranipalEyeData = *TempEyeDataAddr;
+						//UE_LOG(LogTemp, Warning, TEXT("Tried to get EyeData, Result: %d"), Result);
+						SranipalEyeData = LocalEyeData;
 						bDataLogged = false;
 						bIsAsyncEyeTrackingTaskRunning = false;
-						UE_LOG(LogTemp, Warning, TEXT("Tried to get EyeData, Result: %d"), Result);
+						//UE_LOG(LogTemp, Warning, TEXT("finished async task"));
 					});
 	
 			});		
-- 
GitLab