GPT Dialogue System Documentation Portal
  • Welcome
  • Getting Started
    • How to install the plugin
    • Creating an account or logging in
    • GPT Credits
  • In Depth Guides
    • GPT Dialogue Settings
    • How to use in Blueprints
    • How to Use in C++
    • Discord Integration Guide
    • Seeking Support
Powered by GitBook
On this page
  • Adding the Component(Header File)
  • Adding the Component(CPP file)
  • Accessing the Component for its functions
  • Generating Text and Voice Dialogue in C++
  1. In Depth Guides

How to Use in C++

Blueprints logic is the same as C++ and function names are the same, so for this I will just show you how to access the game instance subsystem used for generating, assigning callbacks, and adding a AC_GPTDialogue Component to your actor class.

Feel free to copy the code below.

Adding the Component(Header File)

class UAC_GPTDialogue;

UCLASS()
class GPTDIALOGUE_API AGPTDialogueActor : public AActor
{
    GENERATED_BODY()
    
public:	
    AGPTDialogueActor();

protected:
    virtual void BeginPlay() override;

public:	
    virtual void Tick(float DeltaTime) override;

private:
    UPROPERTY(VisibleAnywhere, BlueprintReadOnly, meta = (AllowPrivateAccess = "true"))
    UAC_GPTDialogue* GPTDialogueComponent;
};

Adding the Component(CPP file)

#include "AC_GPTDialogue.h"

AGPTDialogueActor::AGPTDialogueActor()
{
    PrimaryActorTick.bCanEverTick = true;

    GPTDialogueComponent = CreateDefaultSubobject<UAC_GPTDialogue>(TEXT("GPTDialogueComponent"));
}

Accessing the Component for its functions

void AGPTDialogueActor::GetDialogue()
{
    if (GPTDialogueComponent)
    {
    
    //Ideally, this string would be gotten from the Generate Text Function not manually added.
        FString SampleDialogue = TEXT("Hello there, traveler!");
        GPTDialogueComponent->StoreDialogueHistory(SampleDialogue);
    }
 }

Generating Text and Voice Dialogue in C++

for this we will use a lambda as the callbacks for simplicity sake, but you can link it to other functions if you wish.

#include "GPTDialogueSubsystem.h"
#include "AC_GPTDialogue.h"
#include "Kismet/GameplayStatics.h"
#include "Sound/SoundCue.h"
#include "Sound/SoundAttenuation.h"
#include "Engine/World.h"

void AMyActor::GenerateDialogue()
{
   // Get the dialogue context and prompt
    int32 ContextIndex = 0; // Choose index dynamically if needed
    FGPTDialogueContext Context = GPTDialogueComponent->GetManualDialogueContext(ContextIndex);

    FString Context.FallbackDialogue;
    FString PromptText = GPTDialogueComponent->GenerateDialoguePrompt(Context, FallbackText);

    // Get the subsystem
    if (UGPTDialogueSubsystem* DialogueSubsystem = UGameplayStatics::GetGameInstance(this)->GetSubsystem<UGPTDialogueSubsystem>())
    {
        DialogueSubsystem->GenerateDialogue(PromptText, FallbackText,
        
        
            FDialogueReturn::CreateLambda([=](bool bSuccess, FString ErrorCode, FString GeneratedText)
            {
                if (!bSuccess)
                {
                    UE_LOG(LogTemp, Error, TEXT("Dialogue generation failed: %s"), *ErrorCode);
                    return;
                }

                UE_LOG(LogTemp, Log, TEXT("Generated Dialogue: %s"), *GeneratedText);


                // Generate voice from the generated dialogue
                USoundAttenuation* AttenuationAsset = nullptr; // Replace with asset if needed
                EOAIVoice Voice = EOAIVoice::Nova;

                DialogueSubsystem->GenerateVoiceDialogue(GeneratedText, AttenuationAsset, Voice,
                    FVoiceDialogueReturn::CreateLambda([=](bool bVoiceSuccess, FString VoiceError, USoundCue* VoiceCue)
                    {
                        if (!bVoiceSuccess)
                        {
                            UE_LOG(LogTemp, Error, TEXT("Voice generation failed: %s"), *VoiceError);
                            return;
                        }

                        UE_LOG(LogTemp, Log, TEXT("Voice dialogue generated successfully"));

                        if (VoiceCue)
                        {
                            UGameplayStatics::PlaySound2D(this, VoiceCue);
                        }
                    })
                );
            })
        );
    }
    else
    {
        UE_LOG(LogTemp, Error, TEXT("Failed to get GPTDialogueSubsystem"));
    }
}

That is it, you should now be able to generate text and voice dialogue in c++, you will edit the GPT Context in the editor under the details panel like with the blueprints version.

PreviousHow to use in BlueprintsNextDiscord Integration Guide

Last updated 5 days ago