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);
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,46 +20,46 @@ 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) {
void FWidgetInteractionModule::OnWorldPreActorTick(UWorld* World, ELevelTick TickType, float DeltaTime) {
auto world = world_context.World();
if (last_world == World)
return;
if (last_world == world && widget_interaction_cmp_ != nullptr) {
if (widget_interaction_cmp_->IsValidLowLevel() == true) {
continue;
}
else {
widget_interaction_cmp_ = nullptr;
}
if (World->GetFirstPlayerController() == nullptr)
{
UE_LOG(WidgetIntLog, Error, TEXT("No PlayerController available in OnWorldPreActorTick"));
return;
}
if (world == nullptr)
continue;
auto player_controller = world->GetFirstPlayerController();
if (player_controller == nullptr)
continue;
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;
}
CreateWidgetInteraction(VRPawn->GetRightHandComponent(), VRPawn);
last_world = World;
UE_LOG(WidgetIntLog, Verbose, TEXT("VRInteractionComponent attached to right hand"));
}
}
UVRWidgetInteractionComponent* FWidgetInteractionModule::GetWidgetInteractionComponent() {
if (widget_interaction_cmp_->IsValidLowLevel() == true)
{
return widget_interaction_cmp_;
}
else
{
return nullptr;
}
}
void FWidgetInteractionModule::CreateWidgetInteraction(USceneComponent* parent, AVirtualRealityPawn* outer)
{
......
......@@ -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;
};
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment