Text Generation
Generate AI-powered text responses in Unreal Engine
Text Generation
UPlayKitChatClient is an Actor Component for AI text generation. Add it to any Actor in the editor, configure it in the Details panel, bind to its events, and call GenerateText().
Setup
Blueprint
- Select your Actor and click Add Component → search for PlayKit Chat Client
- In the Details panel, set System Prompt and optionally adjust Model Name and Temperature
- Click + next to On Chat Response to bind a response handler
C++
Add the component to your Actor and bind events in BeginPlay:
#include "Client/PlayKitChatClient.h"
UCLASS()
class AMyActor : public AActor
{
GENERATED_BODY()
UPROPERTY(VisibleAnywhere)
UPlayKitChatClient* ChatClient;
virtual void BeginPlay() override;
UFUNCTION()
void HandleChatResponse(const FPlayKitChatResponse& Response);
};
void AMyActor::BeginPlay()
{
Super::BeginPlay();
ChatClient = FindComponentByClass<UPlayKitChatClient>();
ChatClient->SystemPrompt = TEXT("You are a wise wizard named Merlin.");
ChatClient->OnChatResponse.AddDynamic(this, &AMyActor::HandleChatResponse);
ChatClient->OnError.AddDynamic(this, &AMyActor::HandleError);
}
void AMyActor::HandleChatResponse(const FPlayKitChatResponse& Response)
{
if (Response.bSuccess)
UE_LOG(LogTemp, Log, TEXT("Response: %s"), *Response.Content);
}Basic Text Generation
ChatClient->GenerateText(TEXT("Tell me about dragons"));The SystemPrompt property on the component sets the AI's personality for all requests. You can also change it at runtime:
ChatClient->SystemPrompt = TEXT("You are a fantasy loremaster.");
ChatClient->GenerateText(TEXT("What ancient secrets do you know?"));Streaming Responses
Receive text in real-time as it generates, ideal for typewriter effects.
Blueprint
Bind On Stream Chunk and On Stream Complete events, then call Generate Text Stream.
C++
void AMyActor::BeginPlay()
{
Super::BeginPlay();
ChatClient = FindComponentByClass<UPlayKitChatClient>();
ChatClient->OnStreamChunk.AddDynamic(this, &AMyActor::HandleChunk);
ChatClient->OnStreamComplete.AddDynamic(this, &AMyActor::HandleComplete);
}
void AMyActor::HandleChunk(const FString& Chunk)
{
DialogueWidget->AppendText(Chunk);
}
void AMyActor::StartStream()
{
ChatClient->GenerateTextStream(TEXT("Write a short story about a knight"));
}Advanced Configuration
For full control over messages and settings, use GenerateTextAdvanced():
FPlayKitChatConfig Config;
Config.Messages.Add({ TEXT("system"), TEXT("You are a game master.") });
Config.Messages.Add({ TEXT("user"), TEXT("What quest awaits me?") });
Config.Temperature = 0.9f;
Config.MaxTokens = 200;
ChatClient->GenerateTextAdvanced(Config);Configuration Properties
| Property | Type | Default | Description |
|---|---|---|---|
ModelName | FString | "default-chat" | AI model to use |
Temperature | float | 0.7 | Response randomness (0.0–2.0) |
MaxTokens | int32 | 0 | Max tokens to generate (0 = no limit) |
SystemPrompt | FString | — | Default system prompt |
Runtime setters: SetModelName(), SetTemperature().
Events
| Event | Parameters | Description |
|---|---|---|
OnChatResponse | FPlayKitChatResponse Response | Fired when non-streaming response is received |
OnStreamChunk | FString Chunk | Fired for each chunk during streaming |
OnStreamComplete | FString FullContent | Fired when streaming finishes |
OnError | FString ErrorCode, FString ErrorMessage | Fired on request failure |
Multi-turn Conversations
UPlayKitChatClient is stateless — it does not maintain history between calls. For NPCs that remember conversation context, use UPlayKitNPCClient instead. See NPC Conversations.
Next Steps
- NPC Conversations — dialogue with automatic history management
- Image Generation — AI image generation
- API Reference — complete SDK reference