Updated Interaction.cpp

Optimized more code by caching more widgets and instead of creating new widgets I am just toggling them on and off.
This commit is contained in:
MARCEL HARA 2023-01-23 17:41:55 +00:00
parent c58d5d5592
commit ddc2ef526d
2 changed files with 29 additions and 6 deletions

View File

@ -22,10 +22,27 @@ AInteraction::AInteraction()
void AInteraction::BeginPlay() void AInteraction::BeginPlay()
{ {
Super::BeginPlay(); Super::BeginPlay();
//Character refs
TempCharacter = GetWorld()->GetFirstPlayerController()->GetCharacter(); TempCharacter = GetWorld()->GetFirstPlayerController()->GetCharacter();
MainCamera = Cast<UCameraComponent>(TempCharacter->FindComponentByClass<UCameraComponent>()); MainCamera = Cast<UCameraComponent>(TempCharacter->FindComponentByClass<UCameraComponent>());
//Item refs
TargetHealingLocation = HealingItem->GetActorLocation(); TargetHealingLocation = HealingItem->GetActorLocation();
TargetBuffLocation = BuffItem->GetActorLocation(); TargetBuffLocation = BuffItem->GetActorLocation();
// Dialog refs
ShopDialogWidget = CreateWidget<UUserWidget>(GetWorld(), ShopDialog);
ShopDialogWidget->SetVisibility(ESlateVisibility::Hidden);
ShopDialogWidget->AddToViewport();
//Item Selector refs
ItemSelectorWidget = CreateWidget<UUserWidget>(GetWorld(), ItemSelector);
ItemSelectorWidget->SetVisibility(ESlateVisibility::Hidden);
ItemSelectorWidget->AddToViewport();
Property = ItemSelectorWidget->GetClass()->FindPropertyByName("publicActor");
} }
// Called every frame // Called every frame
@ -38,14 +55,17 @@ void AInteraction::Tick(float DeltaTime)
void AInteraction::OnInteract() void AInteraction::OnInteract()
{ {
bDisableShopDialMove = true; bDisableShopDialMove = true;
ShopDialogWidget = CreateWidget<UUserWidget>(GetWorld(), ShopDialog);
if (ShopDialogWidget == nullptr) if (ShopDialogWidget == nullptr)
{ {
UE_LOG(LogTemp, Display, TEXT("ShopDialogWidget is null"));
return; return;
} }
else else
{ {
ShopDialogWidget->AddToViewport(0); //Shop Widget to visible
ShopDialogWidget->SetVisibility(ESlateVisibility::Visible);
//Call a custom event in a blueprint called "PlayText"
ShopDialogWidget->CallFunctionByNameWithArguments(TEXT("PlayText"), *GLog, nullptr, true);
} }
//handles the widget disappearing from the viewport //handles the widget disappearing from the viewport
FTimerHandle WidgetTimer; FTimerHandle WidgetTimer;
@ -56,10 +76,10 @@ void AInteraction::OnInteract()
void AInteraction::RemoveWidget() void AInteraction::RemoveWidget()
{ {
bisDisabled = true; bisDisabled = true;
ShopDialogWidget->RemoveFromViewport(); //Setting ShopWidgetText back to hidden
ShopDialogWidget->SetVisibility(ESlateVisibility::Hidden);
UE_LOG(LogTemp, Display, TEXT("setting isDisabled to true")); UE_LOG(LogTemp, Display, TEXT("setting isDisabled to true"));
ItemSelectorWidget = CreateWidget<UUserWidget>(GetWorld(), ItemSelector);
FProperty* Property = ItemSelectorWidget->GetClass()->FindPropertyByName("publicActor");
if (Property == nullptr) if (Property == nullptr)
{ {
UE_LOG(LogTemp, Error, TEXT("Property not found")); UE_LOG(LogTemp, Error, TEXT("Property not found"));
@ -75,7 +95,7 @@ void AInteraction::RemoveWidget()
} }
else else
{ {
ItemSelectorWidget->AddToViewport(0); ItemSelectorWidget->SetVisibility(ESlateVisibility::Visible);
bisDisabled = false; bisDisabled = false;
} }
} }

View File

@ -67,10 +67,13 @@ public:
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Items") UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Items")
AActor* BuffItem; AActor* BuffItem;
private: private:
FVector TargetHealingLocation; FVector TargetHealingLocation;
FVector TargetBuffLocation; FVector TargetBuffLocation;
FVector CameraLocation; FVector CameraLocation;
FProperty* Property;
}; };