diff --git a/Source/WidgetInteraction/Private/WidgetInteraction.cpp b/Source/WidgetInteraction/Private/WidgetInteraction.cpp index c35cd28584c7a34b85e3282f37011aa8c5f7449a..fdec676d2ed94f0cc0f67ff02ca3720f30a433c6 100644 --- a/Source/WidgetInteraction/Private/WidgetInteraction.cpp +++ b/Source/WidgetInteraction/Private/WidgetInteraction.cpp @@ -12,8 +12,7 @@ DEFINE_LOG_CATEGORY(WidgetIntLog); void FWidgetInteractionModule::StartupModule() { - on_world_tick_start_delegate_.BindRaw(this, &FWidgetInteractionModule::OnWorldTickStart); - FWorldDelegates::OnWorldTickStart.Add(on_world_tick_start_delegate_); + FWorldDelegates::OnWorldPreActorTick.AddRaw(this, &FWidgetInteractionModule::OnWorldPreActorTick); } void FWidgetInteractionModule::ShutdownModule() @@ -21,45 +20,45 @@ void FWidgetInteractionModule::ShutdownModule() } -void FWidgetInteractionModule::OnWorldTickStart(ELevelTick level_tick, float val) -{ - //since OnWorldTickStart is called independent of the world/level we are in, - //we need to check whether the level changed and, if so, reattach the interaction component - auto worlds = GEngine->GetWorldContexts(); - - 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; - } - } +void FWidgetInteractionModule::OnWorldPreActorTick(UWorld* World, ELevelTick TickType, float DeltaTime) { - if (world == nullptr) - continue; + if (last_world == World) + return; - auto player_controller = world->GetFirstPlayerController(); - if (player_controller == nullptr) - continue; + if (World->GetFirstPlayerController() == nullptr) + { + UE_LOG(WidgetIntLog, Error, TEXT("No PlayerController available in OnWorldPreActorTick")); + return; + } - auto vr_pawn = dynamic_cast<AVirtualRealityPawn*>(player_controller->AcknowledgedPawn); - if (vr_pawn == nullptr) - continue; + APawn* Pawn = World->GetFirstPlayerController()->AcknowledgedPawn; + if (Pawn == nullptr) + { + UE_LOG(WidgetIntLog, Error, TEXT("No Pawn available in OnWorldPreActorTick")); + return; + } - CreateWidgetInteraction(vr_pawn->GetRightHandComponent(), vr_pawn); - last_world = world; + AVirtualRealityPawn* VRPawn = Cast<AVirtualRealityPawn>(Pawn); + if (VRPawn == nullptr) + { + UE_LOG(WidgetIntLog, Error, TEXT("Pawn is not of type AVirtualRealityPawn in OnWorldPreActorTick")); + return; + } - UE_LOG(WidgetIntLog, Verbose, TEXT("VRInteractionComponent attached to right hand")); - } + CreateWidgetInteraction(VRPawn->GetRightHandComponent(), VRPawn); + last_world = World; + UE_LOG(WidgetIntLog, Verbose, TEXT("VRInteractionComponent attached to right hand")); } UVRWidgetInteractionComponent* FWidgetInteractionModule::GetWidgetInteractionComponent() { - return widget_interaction_cmp_; + if (widget_interaction_cmp_->IsValidLowLevel() == true) + { + return widget_interaction_cmp_; + } + else + { + return nullptr; + } } void FWidgetInteractionModule::CreateWidgetInteraction(USceneComponent* parent, AVirtualRealityPawn* outer) diff --git a/Source/WidgetInteraction/Public/WidgetInteraction.h b/Source/WidgetInteraction/Public/WidgetInteraction.h index d1403ee72a7a1d1c53a6b793b7808ca6da01156f..f3f8f271f6bc5c346d1b26481a28f96f435817be 100644 --- a/Source/WidgetInteraction/Public/WidgetInteraction.h +++ b/Source/WidgetInteraction/Public/WidgetInteraction.h @@ -5,6 +5,7 @@ #include "CoreMinimal.h" #include "VirtualRealityPawn.h" #include "VRWidgetInteractionComponent.h" +#include "Engine/World.h" #include "Modules/ModuleManager.h" DECLARE_LOG_CATEGORY_EXTERN(WidgetIntLog, Log, All); @@ -17,7 +18,7 @@ public: virtual void StartupModule() override; virtual void ShutdownModule() override; - UFUNCTION() void OnWorldTickStart(ELevelTick, float); + UFUNCTION() void OnWorldPreActorTick(UWorld* World, ELevelTick TickType, float DeltaTime); UVRWidgetInteractionComponent* GetWidgetInteractionComponent(); @@ -25,8 +26,7 @@ private: void CreateWidgetInteraction(USceneComponent* parent, AVirtualRealityPawn* outer); private: - TBaseDelegate<void, ELevelTick, float> on_world_tick_start_delegate_; UVRWidgetInteractionComponent* widget_interaction_cmp_; - UWorld* last_world; + UWorld* last_world = nullptr; };