// Fill out your copyright notice in the Description page of Project Settings. #include "DialogueNPC.h" #include "Blueprint/UserWidget.h" #include "Components/TextBlock.h" #include "GameFramework/Character.h" // Sets default values for this component's properties UDialogueNPC::UDialogueNPC() { // Set this component to be initialized when the game starts, and to be ticked every frame. You can turn these features // off to improve performance if you don't need them. PrimaryComponentTick.bCanEverTick = true; static ConstructorHelpers::FClassFinder DialogueWidgetClass(TEXT("/Game/Dialogue/TextPrompt")); DialogueWidget = DialogueWidgetClass.Class; } // Called when the game starts void UDialogueNPC::BeginPlay() { Super::BeginPlay(); DialogueWidgetInstance = CreateWidget(GetWorld(), DialogueWidget); NPCNameText = Cast(DialogueWidgetInstance->GetWidgetFromName("Text_Name")); DialogueText = Cast(DialogueWidgetInstance->GetWidgetFromName("Text_Dialogue")); NextButton = Cast(DialogueWidgetInstance->GetWidgetFromName("Button_Next")); NextButton->OnClicked.AddDynamic(this, &UDialogueNPC::NextDialogue); } void UDialogueNPC::NextDialogue() { //Dialogue Skip if (CurrentDialogue.Len() < CurrentDialoguePath[DialogueIndex].Len()) { CurrentDialogue = CurrentDialoguePath[DialogueIndex]; DialogueText->SetText(FText::FromString(CurrentDialogue)); return; } DialogueIndex++; if (DialogueIndex >= CurrentDialoguePath.Num()) { EndDialogue(); return; } CurrentDialogue = ""; } void UDialogueNPC::NextCharacter() { if (DialogueIndex >= CurrentDialoguePath.Num()) return; if (CurrentDialogue.Len() < CurrentDialoguePath[DialogueIndex].Len()) { CurrentDialogue.AppendChar(CurrentDialoguePath[DialogueIndex][CurrentDialogue.Len()]); DialogueText->SetText(FText::FromString(CurrentDialogue)); } } // Called every frame void UDialogueNPC::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) { Super::TickComponent(DeltaTime, TickType, ThisTickFunction); // ... } void UDialogueNPC::StartDialogue() { //Disable Character Movement if (ACharacter* PlayerCharacter = Cast(GetWorld()->GetFirstPlayerController()->GetPawn())) { PlayerCharacter->DisableInput(GetWorld()->GetFirstPlayerController()); } //Set to UI Mode Only APlayerController* PlayerController = GetWorld()->GetFirstPlayerController(); PlayerController->SetInputMode(FInputModeUIOnly()); PlayerController->bShowMouseCursor = true; DialogueWidgetInstance->AddToViewport(); DialogueIndex = 1; NPCNameText->SetText(FText::FromString(NPCName)); CurrentDialogue = ""; CurrentDialoguePath = DialoguePaths[0].Dialogue; GetWorld()->GetTimerManager().SetTimer(TextAnimationTimerHandle, this, &UDialogueNPC::NextCharacter, TextAnimationSpeed, true); } void UDialogueNPC::EndDialogue() { TextAnimationTimerHandle.Invalidate(); DialogueWidgetInstance->RemoveFromParent(); //Enable Character Movement if (ACharacter* PlayerCharacter = Cast(GetWorld()->GetFirstPlayerController()->GetPawn())) { PlayerCharacter->EnableInput(GetWorld()->GetFirstPlayerController()); } //Reset UI Mode APlayerController* PlayerController = GetWorld()->GetFirstPlayerController(); PlayerController->SetInputMode(FInputModeGameOnly()); PlayerController->bShowMouseCursor = false; } FDialoguePath UDialogueNPC::CreateRootDialoguePath() { return FDialoguePath(); } 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) { if (TextInput.IsEmpty()) return DialoguePath; DialoguePath.Dialogue.Add(TextInput.ToString()); return DialoguePath; } void UDialogueNPC::GetFinalDialogue(TArray DialogueArray) { DialoguePaths.Add(FDialoguePath(DialogueArray)); }