Lingara Lingara Docs Guides API Libraries Apps Build Embed Web app
Language: English

Unreal

The Lingara plugin for Unreal Engine 5 is a game-instance subsystem over the player core’s C interface. It runs on the five platforms the player kits support: Windows, macOS, Linux, Android and iOS. Consoles are not supported yet.

Install the plugin

Each release carries a zip of the plugin for each supported engine version, named LingaraEmbed-<semver>-UE5.<m>.zip. Unzip the one for your engine into your project’s Plugins/ folder, then enable the plugin in the editor. There is nothing else to install.

Get tokens from your server

The plugin never holds a secret. When it needs a token, it broadcasts OnTokenNeeded. Answer it by posting to your own server’s token route, which mints with a server kit and answers with the token and its expiry, then hand both to ProvideToken. If your server cannot answer, call TokenUnavailable so the calls waiting on a token fail at once instead of timing out.

Bind the events you use

The core runs its network I/O on its own thread. The subsystem drains it from its own tick, at most MaxEventsPerTick events a frame (a project setting), and broadcasts each one on a delegate. Bind the delegates your game needs when play begins, then start the subsystem:

  • OnTokenNeeded, when a token is needed;
  • OnNpcTurn, for each piece of a streamed reply (see NPC dialogue);
  • OnPractice, for each practice update (see practice results);
  • OnError, when a call fails.

Unreal C++

// Your own server mints the token: never put a client secret in a game.
void ALingaraEmbedSnippetActor::HandleTokenNeeded(const FLingaraTokenNeeded& Event)
{
	const TSharedRef<IHttpRequest, ESPMode::ThreadSafe> Request = FHttpModule::Get().CreateRequest();
	Request->SetVerb(TEXT("POST"));
	Request->SetURL(TokenUrl);
	Request->SetHeader(TEXT("Content-Type"), TEXT("application/json"));
	Request->SetContentAsString(TEXT("{}"));
	Request->OnProcessRequestComplete().BindUObject(this, &ALingaraEmbedSnippetActor::HandleTokenResponse);
	Request->ProcessRequest();
}

void ALingaraEmbedSnippetActor::HandleTokenResponse(FHttpRequestPtr Request, FHttpResponsePtr Response, bool bConnected)
{
	ELingaraCallResult Result;
	FLingaraEmbedError Error;
	TSharedPtr<FJsonObject> Body;
	FString Token;
	FString ExpiresAt;
	const bool bOk = bConnected && Response.IsValid() && EHttpResponseCodes::IsOk(Response->GetResponseCode())
		&& FJsonSerializer::Deserialize(TJsonReaderFactory<TCHAR>::Create(Response->GetContentAsString()), Body)
		&& Body->TryGetStringField(TEXT("token"), Token) && Body->TryGetStringField(TEXT("expires_at"), ExpiresAt);
	if (bOk)
	{
		Lingara()->ProvideToken(Token, ExpiresAt, Result, Error);
	}
	else
	{
		// Fail the waiting calls now rather than at the core's timeout.
		Lingara()->TokenUnavailable(TEXT("the token endpoint failed"), Result, Error);
	}
}

Unreal C++

// The subsystem drains the core's queue from its own tick (at most
// MaxEventsPerTick a frame, from Project Settings): bind the events you use.
void ALingaraEmbedSnippetActor::BeginPlay()
{
	Super::BeginPlay();
	ULingaraEmbedSubsystem* Subsystem = Lingara();
	Subsystem->OnTokenNeeded.AddDynamic(this, &ALingaraEmbedSnippetActor::HandleTokenNeeded);
	Subsystem->OnNpcTurn.AddDynamic(this, &ALingaraEmbedSnippetActor::HandleNpcTurn);
	Subsystem->OnPractice.AddDynamic(this, &ALingaraEmbedSnippetActor::HandlePractice);
	Subsystem->OnError.AddDynamic(this, &ALingaraEmbedSnippetActor::HandleError);

	FLingaraEmbedConfig Config;
	Config.MaxEventsPerTick = 256;
	ELingaraCallResult Result;
	FLingaraEmbedError Error;
	Subsystem->Start(Config, Result, Error);
}

void ALingaraEmbedSnippetActor::HandleError(const FLingaraEmbedError& Error)
{
	UE_LOG(LogTemp, Warning, TEXT("Lingara %s: %s"), *Error.KindName, *Error.Message);
}