From 0dbd80d90cb0acf98a323d3b6418d11b0b1affd4 Mon Sep 17 00:00:00 2001
From: Jonathan Wendt <wendt@vr.rwth-aachen.de>
Date: Wed, 29 May 2019 17:16:35 +0200
Subject: [PATCH] improvement if switching levels

---
 .../Private/WidgetInteraction.cpp             | 49 +++++++++++--------
 .../Public/WidgetInteraction.h                |  1 +
 2 files changed, 30 insertions(+), 20 deletions(-)

diff --git a/Source/WidgetInteraction/Private/WidgetInteraction.cpp b/Source/WidgetInteraction/Private/WidgetInteraction.cpp
index 7d9f0a8..e328617 100644
--- a/Source/WidgetInteraction/Private/WidgetInteraction.cpp
+++ b/Source/WidgetInteraction/Private/WidgetInteraction.cpp
@@ -21,20 +21,26 @@ void FWidgetInteractionModule::ShutdownModule()
 
 void FWidgetInteractionModule::OnWorldTickStart(ELevelTick level_tick, float val)
 {
+  //called every Tick()
+
   auto worlds = GEngine->GetWorldContexts();
 
-  if (widget_interaction_cmp_ != nullptr) {
-    if (widget_interaction_cmp_->IsValidLowLevel() == true) {
-      return;
-    }
-    else {
-      widget_interaction_cmp_ = nullptr;
-    }
-  }
+  for (auto world_context : worlds) {
 
-  for (auto & world_context : worlds)
-  {
     auto world = world_context.World();
+
+    if (last_world == world  && widget_interaction_cmp_ != nullptr) {
+      if (widget_interaction_cmp_->IsValidLowLevel() == true) {
+        continue;
+      }
+      else {
+        widget_interaction_cmp_ = nullptr;
+      }
+    }
+
+    if (world == nullptr)
+      continue;
+
     auto player_controller = world->GetFirstPlayerController();
     if (player_controller == nullptr)
       continue;
@@ -43,6 +49,8 @@ void FWidgetInteractionModule::OnWorldTickStart(ELevelTick level_tick, float val
     if (vr_pawn == nullptr)
       continue;
 
+    UE_LOG(LogTemp, Warning, TEXT("OnWorldTickStart called and interaction component will be updated"));
+
     FString name = "";
     UClass* component_class = UMotionControllerComponent::StaticClass();
 
@@ -67,17 +75,18 @@ void FWidgetInteractionModule::OnWorldTickStart(ELevelTick level_tick, float val
     }
 
     auto parent_vec = vr_pawn->GetComponentsByClass(component_class);
-	bool success;
-    for (auto parent : parent_vec)
-      if (parent->GetName() == FString(name))
-      {
-		CreateWidgetInteraction(dynamic_cast<USceneComponent*>(parent), vr_pawn);
-		success = true;
-	  }
-	if(!success)
-		UE_LOG(LogTemp, Error, TEXT("Failed to load widget asset \"%s"), *name);
-  }
+    bool success;
+    for (auto parent : parent_vec) {
+      if (parent->GetName() == FString(name)) {
+        CreateWidgetInteraction(dynamic_cast<USceneComponent*>(parent), vr_pawn);
+        success = true;
+        last_world = world;
+      }
+    }
 
+    if (!success)
+      UE_LOG(LogTemp, Error, TEXT("Failed to load widget asset \"%s"), *name);
+  }
 }
 
 void FWidgetInteractionModule::CreateWidgetInteraction(USceneComponent * parent, AVirtualRealityPawn* outer)
diff --git a/Source/WidgetInteraction/Public/WidgetInteraction.h b/Source/WidgetInteraction/Public/WidgetInteraction.h
index 6a604d6..747a3bd 100644
--- a/Source/WidgetInteraction/Public/WidgetInteraction.h
+++ b/Source/WidgetInteraction/Public/WidgetInteraction.h
@@ -25,4 +25,5 @@ private:
 	TBaseDelegate<void, ELevelTick, float> on_world_tick_start_delegate_;
 
 	URwthComponent * widget_interaction_cmp_;
+        UWorld* last_world;
 };
-- 
GitLab