Updated Dialogue for Choices

This commit is contained in:
Philip W 2023-03-13 04:49:09 +00:00
parent d2325b514c
commit 76b2d78f0e
7 changed files with 152 additions and 94 deletions

Binary file not shown.

BIN
Content/Dialogue/BungeeManPortrait.uasset (Stored with Git LFS) Normal file

Binary file not shown.

BIN
Content/Dialogue/DialogueTest.umap (Stored with Git LFS)

Binary file not shown.

BIN
Content/Dialogue/NPCTest.uasset (Stored with Git LFS)

Binary file not shown.

BIN
Content/Dialogue/TextPrompt.uasset (Stored with Git LFS)

Binary file not shown.

View File

@ -29,23 +29,45 @@ void UDialogueNPC::BeginPlay()
DialogueText = Cast<UTextBlock>(DialogueWidgetInstance->GetWidgetFromName("Text_Dialogue"));
NextButton = Cast<UButton>(DialogueWidgetInstance->GetWidgetFromName("Button_Next"));
NextButton->OnClicked.AddDynamic(this, &UDialogueNPC::NextDialogue);
NPCPortraitImage = Cast<UImage>(DialogueWidgetInstance->GetWidgetFromName("Image_Portrait"));
if (IsValid(NPCPortrait)) NPCPortraitImage->SetBrushFromTexture(NPCPortrait);
Choice1Button = Cast<UButton>(DialogueWidgetInstance->GetWidgetFromName("Button_Choice1"));
Choice1Button->OnClicked.AddDynamic(this, &UDialogueNPC::Choice1);
Choice2Button = Cast<UButton>(DialogueWidgetInstance->GetWidgetFromName("Button_Choice2"));
Choice2Button->OnClicked.AddDynamic(this, &UDialogueNPC::Choice2);
Choice3Button = Cast<UButton>(DialogueWidgetInstance->GetWidgetFromName("Button_Choice3"));
Choice3Button->OnClicked.AddDynamic(this, &UDialogueNPC::Choice3);
Choice1Text = Cast<UTextBlock>(DialogueWidgetInstance->GetWidgetFromName("Text_Choice1"));
Choice2Text = Cast<UTextBlock>(DialogueWidgetInstance->GetWidgetFromName("Text_Choice2"));
Choice3Text = Cast<UTextBlock>(DialogueWidgetInstance->GetWidgetFromName("Text_Choice3"));
}
void UDialogueNPC::NextDialogue()
{
//Dialogue Skip
if (CurrentDialogue.Len() < CurrentDialoguePath[DialogueIndex].Len())
if (CurrentDialogue.Len() < CurrentDialogueStringPath[DialogueIndex].Len())
{
CurrentDialogue = CurrentDialoguePath[DialogueIndex];
CurrentDialogue = CurrentDialogueStringPath[DialogueIndex];
DialogueText->SetText(FText::FromString(CurrentDialogue));
return;
}
DialogueIndex++;
if (DialogueIndex >= CurrentDialoguePath.Num())
if (DialogueIndex >= CurrentDialogueStringPath.Num())
{
EndDialogue();
if (CurrentDialoguePath->Choices.IsEmpty())
{
EndDialogue();
return;
}
GetWorld()->GetTimerManager().PauseTimer(TextAnimationTimerHandle);
DialogueText->SetText(FText::FromString(""));
Choice1Button->SetVisibility(ESlateVisibility::Visible);
Choice2Button->SetVisibility(ESlateVisibility::Visible);
if (Choice3Text->GetText().ToString() != "") Choice3Button->SetVisibility(ESlateVisibility::Visible);
NextButton->SetVisibility(ESlateVisibility::Hidden);
return;
}
CurrentDialogue = "";
@ -53,11 +75,11 @@ void UDialogueNPC::NextDialogue()
void UDialogueNPC::NextCharacter()
{
if (DialogueIndex >= CurrentDialoguePath.Num()) return;
if (DialogueIndex >= CurrentDialogueStringPath.Num()) return;
if (CurrentDialogue.Len() < CurrentDialoguePath[DialogueIndex].Len())
if (CurrentDialogue.Len() < CurrentDialogueStringPath[DialogueIndex].Len())
{
CurrentDialogue.AppendChar(CurrentDialoguePath[DialogueIndex][CurrentDialogue.Len()]);
CurrentDialogue.AppendChar(CurrentDialogueStringPath[DialogueIndex][CurrentDialogue.Len()]);
DialogueText->SetText(FText::FromString(CurrentDialogue));
}
}
@ -72,6 +94,12 @@ void UDialogueNPC::TickComponent(float DeltaTime, ELevelTick TickType, FActorCom
void UDialogueNPC::StartDialogue()
{
if (IsValid(RootDialoguePath)) CurrentDialogueStringPath = RootDialoguePath->Dialogue;
if (CurrentDialogueStringPath.IsEmpty())
{
UE_LOG(LogTemp, Warning, TEXT("Dialogue Path is Empty"));
return;
}
//Disable Character Movement
if (ACharacter* PlayerCharacter = Cast<ACharacter>(GetWorld()->GetFirstPlayerController()->GetPawn()))
{
@ -82,11 +110,16 @@ void UDialogueNPC::StartDialogue()
PlayerController->SetInputMode(FInputModeUIOnly());
PlayerController->bShowMouseCursor = true;
Choice1Button->SetVisibility(ESlateVisibility::Hidden);
Choice2Button->SetVisibility(ESlateVisibility::Hidden);
Choice3Button->SetVisibility(ESlateVisibility::Hidden);
DialogueText->SetText(FText::FromString(""));
DialogueWidgetInstance->AddToViewport();
DialogueIndex = 1;
NPCNameText->SetText(FText::FromString(NPCName));
DialogueIndex = 0;
CurrentDialogue = "";
CurrentDialoguePath = DialoguePaths[0].Dialogue;
CurrentDialoguePath = RootDialoguePath;
CurrentDialogueStringPath = RootDialoguePath->Dialogue;
GetWorld()->GetTimerManager().SetTimer(TextAnimationTimerHandle, this, &UDialogueNPC::NextCharacter, TextAnimationSpeed, true);
}
@ -106,42 +139,67 @@ void UDialogueNPC::EndDialogue()
PlayerController->bShowMouseCursor = false;
}
FDialoguePath UDialogueNPC::CreateRootDialoguePath()
UDialoguePath* UDialogueNPC::CreateRootDialoguePath()
{
return FDialoguePath();
RootDialoguePath = NewObject<UDialoguePath>();
return RootDialoguePath;
}
FDialoguePath UDialogueNPC::CreateDialoguePath(FDialoguePath ParentDialoguePath)
{
return FDialoguePath("#" + FString::FromInt(DialoguePaths.Num()));
}
FDialoguePath UDialogueNPC::CreateDialogueChoice(FDialoguePath PreviousDialoguePath)
{
return FDialoguePath();
}
bool UDialogueNPC::GotoDialoguePath(FString PathId)
{
for (FDialoguePath DialogPath : DialoguePaths)
{
if (DialogPath.Dialogue[0] == PathId)
{
CurrentDialoguePath = DialogPath.Dialogue;
return true;
}
}
return false;
}
FDialoguePath UDialogueNPC::AddDialogue(FText TextInput, FDialoguePath DialoguePath)
UDialoguePath* UDialogueNPC::AddDialogue(UDialoguePath* DialoguePath, FText TextInput)
{
if (TextInput.IsEmpty()) return DialoguePath;
DialoguePath.Dialogue.Add(TextInput.ToString());
DialoguePath->Dialogue.Add(TextInput.ToString());
return DialoguePath;
}
void UDialogueNPC::GetFinalDialogue(TArray<FString> DialogueArray)
void UDialogueNPC::AddChoices(UDialoguePath* ParentPath, FText ChoiceText1, FText ChoiceText2, FText ChoiceText3, UDialoguePath*& Out_ChoicePath1, UDialoguePath*& Out_ChoicePath2, UDialoguePath*& Out_ChoicePath3)
{
DialoguePaths.Add(FDialoguePath(DialogueArray));
ParentPath->Choices.Add(NewObject<UDialoguePath>());
ParentPath->Choices.Add(NewObject<UDialoguePath>());
ParentPath->Choices.Add(NewObject<UDialoguePath>());
Out_ChoicePath1 = ParentPath->Choices[0];
Out_ChoicePath2 = ParentPath->Choices[1];
Out_ChoicePath3 = ParentPath->Choices[2];
Choice1Text->SetText(ChoiceText1);
Choice2Text->SetText(ChoiceText2);
Choice3Text->SetText(ChoiceText3);
}
void UDialogueNPC::Choice1()
{
CurrentDialoguePath = CurrentDialoguePath->Choices[0];
DialogueIndex = 0;
CurrentDialogue = "";
CurrentDialogueStringPath = CurrentDialoguePath->Dialogue;
GetWorld()->GetTimerManager().UnPauseTimer(TextAnimationTimerHandle);
Choice1Button->SetVisibility(ESlateVisibility::Hidden);
Choice2Button->SetVisibility(ESlateVisibility::Hidden);
Choice3Button->SetVisibility(ESlateVisibility::Hidden);
NextButton->SetVisibility(ESlateVisibility::Visible);
}
void UDialogueNPC::Choice2()
{
CurrentDialoguePath = CurrentDialoguePath->Choices[1];
DialogueIndex = 0;
CurrentDialogue = "";
CurrentDialogueStringPath = CurrentDialoguePath->Dialogue;
GetWorld()->GetTimerManager().UnPauseTimer(TextAnimationTimerHandle);
Choice1Button->SetVisibility(ESlateVisibility::Hidden);
Choice2Button->SetVisibility(ESlateVisibility::Hidden);
Choice3Button->SetVisibility(ESlateVisibility::Hidden);
NextButton->SetVisibility(ESlateVisibility::Visible);
}
void UDialogueNPC::Choice3()
{
CurrentDialoguePath = CurrentDialoguePath->Choices[2];
DialogueIndex = 0;
CurrentDialogue = "";
CurrentDialogueStringPath = CurrentDialoguePath->Dialogue;
GetWorld()->GetTimerManager().UnPauseTimer(TextAnimationTimerHandle);
Choice1Button->SetVisibility(ESlateVisibility::Hidden);
Choice2Button->SetVisibility(ESlateVisibility::Hidden);
Choice3Button->SetVisibility(ESlateVisibility::Hidden);
NextButton->SetVisibility(ESlateVisibility::Visible);
}

View File

@ -6,38 +6,20 @@
#include "Components/ActorComponent.h"
#include "Components/Button.h"
#include "Components/TextBlock.h"
#include "Components/Image.h"
#include "DialogueNPC.generated.h"
UENUM(BlueprintType)
enum class EChoices : uint8
{
Choice1 UMETA(DisplayName="Choice 1"),
Choice2 UMETA(DisplayName="Choice 2"),
Choice3 UMETA(DisplayName="Choice 3"),
};
USTRUCT(BlueprintType)
struct FDialoguePath
UCLASS(ClassGroup=(Custom), meta=(BlueprintSpawnableComponent))
class UDialoguePath : public UObject
{
GENERATED_BODY()
UPROPERTY()
public:
UPROPERTY(BlueprintReadWrite)
TArray<FString> Dialogue;
FDialoguePath()
{
Dialogue.Add("#ROOT");
}
FDialoguePath(FString Id)
{
Dialogue.Add(Id);
}
explicit FDialoguePath(TArray<FString> Dialogue)
{
this->Dialogue = Dialogue;
}
UPROPERTY(BlueprintReadWrite)
TArray<UDialoguePath*> Choices;
};
UCLASS(ClassGroup=(Custom), meta=(BlueprintSpawnableComponent))
@ -51,11 +33,15 @@ public:
UPROPERTY(EditAnywhere)
FString NPCName;
UPROPERTY()
TArray<FDialoguePath> DialoguePaths;
TArray<FString> CurrentDialoguePath;
UPROPERTY()
UDialoguePath* RootDialoguePath;
UPROPERTY()
UDialoguePath* CurrentDialoguePath;
TArray<FString> CurrentDialogueStringPath;
UPROPERTY(EditAnywhere)
UTexture2D* NPCPortrait;
UPROPERTY(EditAnywhere)
float TextAnimationSpeed = 0.05f;
@ -70,13 +56,26 @@ protected:
private:
UPROPERTY()
UUserWidget* DialogueWidgetInstance;
UPROPERTY()
UTextBlock* NPCNameText;
UPROPERTY()
UTextBlock* NPCNameText;
UPROPERTY()
UTextBlock* DialogueText;
UPROPERTY()
UImage* NPCPortraitImage;
UPROPERTY()
UButton* Choice1Button;
UPROPERTY()
UButton* Choice2Button;
UPROPERTY()
UButton* Choice3Button;
UPROPERTY()
UTextBlock* Choice1Text;
UPROPERTY()
UTextBlock* Choice2Text;
UPROPERTY()
UTextBlock* Choice3Text;
UPROPERTY()
UButton* NextButton;
@ -100,21 +99,19 @@ public:
UFUNCTION(BlueprintCallable)
void EndDialogue();
UFUNCTION(BlueprintPure)
FDialoguePath CreateRootDialoguePath();
UFUNCTION()
FDialoguePath CreateDialoguePath(FDialoguePath ParentDialoguePath);
UFUNCTION(BlueprintPure)
FDialoguePath CreateDialogueChoice(FDialoguePath PreviousDialoguePath);
UFUNCTION()
bool GotoDialoguePath(FString PathId);
UFUNCTION(BlueprintPure)
FDialoguePath AddDialogue(FText TextInput, FDialoguePath DialoguePath);
UFUNCTION(BlueprintCallable)
UDialoguePath* CreateRootDialoguePath();
UFUNCTION(BlueprintCallable)
void GetFinalDialogue(TArray<FString> DialogueArray);
UDialoguePath* AddDialogue(UDialoguePath* DialoguePath, FText TextInput);
UFUNCTION(BlueprintCallable)
void AddChoices(UDialoguePath* ParentPath, FText ChoiceText1, FText ChoiceText2, FText ChoiceText3,
UDialoguePath*& ChoicePath1, UDialoguePath*& ChoicePath2, UDialoguePath*& ChoicePath3);
UFUNCTION()
void Choice1();
UFUNCTION()
void Choice2();
UFUNCTION()
void Choice3();
};