From ddc2ef526dedffc5eba951285ed185efd00d7a46 Mon Sep 17 00:00:00 2001 From: MARCEL HARA Date: Mon, 23 Jan 2023 17:41:55 +0000 Subject: [PATCH] Updated Interaction.cpp Optimized more code by caching more widgets and instead of creating new widgets I am just toggling them on and off. --- .../MerchantInteraction/Interaction.cpp | 32 +++++++++++++++---- .../MerchantInteraction/Interaction.h | 3 ++ 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/Source/the_twilight_abyss/MerchantInteraction/Interaction.cpp b/Source/the_twilight_abyss/MerchantInteraction/Interaction.cpp index f8a95f4..89e3eef 100644 --- a/Source/the_twilight_abyss/MerchantInteraction/Interaction.cpp +++ b/Source/the_twilight_abyss/MerchantInteraction/Interaction.cpp @@ -22,10 +22,27 @@ AInteraction::AInteraction() void AInteraction::BeginPlay() { Super::BeginPlay(); + + //Character refs TempCharacter = GetWorld()->GetFirstPlayerController()->GetCharacter(); MainCamera = Cast(TempCharacter->FindComponentByClass()); + + //Item refs + TargetHealingLocation = HealingItem->GetActorLocation(); TargetBuffLocation = BuffItem->GetActorLocation(); + + // Dialog refs + + ShopDialogWidget = CreateWidget(GetWorld(), ShopDialog); + ShopDialogWidget->SetVisibility(ESlateVisibility::Hidden); + ShopDialogWidget->AddToViewport(); + + //Item Selector refs + ItemSelectorWidget = CreateWidget(GetWorld(), ItemSelector); + ItemSelectorWidget->SetVisibility(ESlateVisibility::Hidden); + ItemSelectorWidget->AddToViewport(); + Property = ItemSelectorWidget->GetClass()->FindPropertyByName("publicActor"); } // Called every frame @@ -38,14 +55,17 @@ void AInteraction::Tick(float DeltaTime) void AInteraction::OnInteract() { bDisableShopDialMove = true; - ShopDialogWidget = CreateWidget(GetWorld(), ShopDialog); if (ShopDialogWidget == nullptr) { + UE_LOG(LogTemp, Display, TEXT("ShopDialogWidget is null")); return; } 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 FTimerHandle WidgetTimer; @@ -56,10 +76,10 @@ void AInteraction::OnInteract() void AInteraction::RemoveWidget() { bisDisabled = true; - ShopDialogWidget->RemoveFromViewport(); + //Setting ShopWidgetText back to hidden + ShopDialogWidget->SetVisibility(ESlateVisibility::Hidden); UE_LOG(LogTemp, Display, TEXT("setting isDisabled to true")); - ItemSelectorWidget = CreateWidget(GetWorld(), ItemSelector); - FProperty* Property = ItemSelectorWidget->GetClass()->FindPropertyByName("publicActor"); + if (Property == nullptr) { UE_LOG(LogTemp, Error, TEXT("Property not found")); @@ -75,7 +95,7 @@ void AInteraction::RemoveWidget() } else { - ItemSelectorWidget->AddToViewport(0); + ItemSelectorWidget->SetVisibility(ESlateVisibility::Visible); bisDisabled = false; } } diff --git a/Source/the_twilight_abyss/MerchantInteraction/Interaction.h b/Source/the_twilight_abyss/MerchantInteraction/Interaction.h index 09e9478..8b9e5a7 100644 --- a/Source/the_twilight_abyss/MerchantInteraction/Interaction.h +++ b/Source/the_twilight_abyss/MerchantInteraction/Interaction.h @@ -66,11 +66,14 @@ public: UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Items") AActor* BuffItem; + private: FVector TargetHealingLocation; FVector TargetBuffLocation; FVector CameraLocation; + + FProperty* Property; };