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;
};
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.