Skip to content
Snippets Groups Projects
Commit 6158bebe authored by Jonathan Wendt's avatar Jonathan Wendt
Browse files

improve setup of widgetinteractioncomponent

parent 1ad51beb
No related branches found
No related tags found
No related merge requests found
...@@ -12,8 +12,7 @@ DEFINE_LOG_CATEGORY(WidgetIntLog); ...@@ -12,8 +12,7 @@ DEFINE_LOG_CATEGORY(WidgetIntLog);
void FWidgetInteractionModule::StartupModule() void FWidgetInteractionModule::StartupModule()
{ {
on_world_tick_start_delegate_.BindRaw(this, &FWidgetInteractionModule::OnWorldTickStart); FWorldDelegates::OnWorldPreActorTick.AddRaw(this, &FWidgetInteractionModule::OnWorldPreActorTick);
FWorldDelegates::OnWorldTickStart.Add(on_world_tick_start_delegate_);
} }
void FWidgetInteractionModule::ShutdownModule() void FWidgetInteractionModule::ShutdownModule()
...@@ -21,46 +20,46 @@ void FWidgetInteractionModule::ShutdownModule() ...@@ -21,46 +20,46 @@ void FWidgetInteractionModule::ShutdownModule()
} }
void FWidgetInteractionModule::OnWorldTickStart(ELevelTick level_tick, float val) void FWidgetInteractionModule::OnWorldPreActorTick(UWorld* World, ELevelTick TickType, float DeltaTime) {
{
//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)
return;
if (last_world == world && widget_interaction_cmp_ != nullptr) { if (World->GetFirstPlayerController() == nullptr)
if (widget_interaction_cmp_->IsValidLowLevel() == true) { {
continue; UE_LOG(WidgetIntLog, Error, TEXT("No PlayerController available in OnWorldPreActorTick"));
} return;
else {
widget_interaction_cmp_ = nullptr;
}
} }
if (world == nullptr) APawn* Pawn = World->GetFirstPlayerController()->AcknowledgedPawn;
continue; if (Pawn == nullptr)
{
auto player_controller = world->GetFirstPlayerController(); UE_LOG(WidgetIntLog, Error, TEXT("No Pawn available in OnWorldPreActorTick"));
if (player_controller == nullptr) return;
continue; }
auto vr_pawn = dynamic_cast<AVirtualRealityPawn*>(player_controller->AcknowledgedPawn);
if (vr_pawn == nullptr)
continue;
CreateWidgetInteraction(vr_pawn->GetRightHandComponent(), vr_pawn); AVirtualRealityPawn* VRPawn = Cast<AVirtualRealityPawn>(Pawn);
last_world = world; if (VRPawn == nullptr)
{
UE_LOG(WidgetIntLog, Error, TEXT("Pawn is not of type AVirtualRealityPawn in OnWorldPreActorTick"));
return;
}
CreateWidgetInteraction(VRPawn->GetRightHandComponent(), VRPawn);
last_world = World;
UE_LOG(WidgetIntLog, Verbose, TEXT("VRInteractionComponent attached to right hand")); UE_LOG(WidgetIntLog, Verbose, TEXT("VRInteractionComponent attached to right hand"));
} }
}
UVRWidgetInteractionComponent* FWidgetInteractionModule::GetWidgetInteractionComponent() { UVRWidgetInteractionComponent* FWidgetInteractionModule::GetWidgetInteractionComponent() {
if (widget_interaction_cmp_->IsValidLowLevel() == true)
{
return widget_interaction_cmp_; return widget_interaction_cmp_;
} }
else
{
return nullptr;
}
}
void FWidgetInteractionModule::CreateWidgetInteraction(USceneComponent* parent, AVirtualRealityPawn* outer) void FWidgetInteractionModule::CreateWidgetInteraction(USceneComponent* parent, AVirtualRealityPawn* outer)
{ {
......
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
#include "CoreMinimal.h" #include "CoreMinimal.h"
#include "VirtualRealityPawn.h" #include "VirtualRealityPawn.h"
#include "VRWidgetInteractionComponent.h" #include "VRWidgetInteractionComponent.h"
#include "Engine/World.h"
#include "Modules/ModuleManager.h" #include "Modules/ModuleManager.h"
DECLARE_LOG_CATEGORY_EXTERN(WidgetIntLog, Log, All); DECLARE_LOG_CATEGORY_EXTERN(WidgetIntLog, Log, All);
...@@ -17,7 +18,7 @@ public: ...@@ -17,7 +18,7 @@ public:
virtual void StartupModule() override; virtual void StartupModule() override;
virtual void ShutdownModule() override; virtual void ShutdownModule() override;
UFUNCTION() void OnWorldTickStart(ELevelTick, float); UFUNCTION() void OnWorldPreActorTick(UWorld* World, ELevelTick TickType, float DeltaTime);
UVRWidgetInteractionComponent* GetWidgetInteractionComponent(); UVRWidgetInteractionComponent* GetWidgetInteractionComponent();
...@@ -25,8 +26,7 @@ private: ...@@ -25,8 +26,7 @@ private:
void CreateWidgetInteraction(USceneComponent* parent, AVirtualRealityPawn* outer); void CreateWidgetInteraction(USceneComponent* parent, AVirtualRealityPawn* outer);
private: private:
TBaseDelegate<void, ELevelTick, float> on_world_tick_start_delegate_;
UVRWidgetInteractionComponent* widget_interaction_cmp_; UVRWidgetInteractionComponent* widget_interaction_cmp_;
UWorld* last_world; UWorld* last_world = nullptr;
}; };
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment