Revolutionize Your Game with AI-Powered NPCs: How to Develop Non-Playable Characters that Interact with Real People on Unreal Engine using OpenAI, InWorld, and ElevenLabs

Javier Calderon Jr
5 min readMar 27, 2023

Unreal Engine is a powerful game development engine that allows game developers to create immersive experiences for players. One way to enhance these experiences is by using OpenAI to develop non-playable characters (NPCs) that interact with real people in natural and engaging ways. Here’s how you can use OpenAI to develop NPCs that interact with real people on Unreal Engine using best practices:

  1. Define your NPC: The first step is to define your NPC’s personality, backstory, goals, and motivations. This will help you create a character that feels like a real person, rather than a generic NPC.
  2. Use OpenAI for natural language interactions: Use OpenAI’s GPT-3 language model to generate natural language responses for your NPC. For example, you can use GPT-3 to create dialogue trees for your NPC, allowing players to have conversations with them.
  3. Use Unreal Engine for dynamic behavior: Use Unreal Engine to create dynamic, non-scripted behavior for your NPC. This will allow your NPC to respond to player actions in real-time, creating a more immersive experience.
  4. Use best practices for NPC development: Use best practices such as creating believable character models, implementing believable animations, and designing an intuitive user interface for your NPC interactions. This will help ensure that your NPC feels like a real person and not a robotic character.
  5. Implement the NPC: Once you have created your NPC, implement it in your game using Unreal Engine. This may involve creating a character model, designing their behavior and interactions with other game objects, and programming their dialogue and decision-making using OpenAI.
  6. Test and refine: Test your NPC in the game environment to ensure that it behaves as expected. You may need to tweak its behavior, dialogue, or decision-making based on player feedback or bugs that you encounter during testing.
  7. Release: Once you are satisfied with your NPC, release your game to the public. Monitor player feedback to see how they interact with your NPC and use this feedback to improve the NPC in future updates.

Overall, creating NPCs that interact with real people using OpenAI and Unreal Engine requires careful planning, implementation, and testing. By following best practices and iterating on your design, you can create NPCs that feel like real characters, enhancing the overall immersion and enjoyment of your game. Whether you are creating a mobile game or a large-scale RPG, incorporating AI-powered NPCs can help take your game to the next level. With the power of OpenAI and Unreal Engine at your fingertips, the possibilities are endless.

Lets start building the NPC using AI tools and Unreal Engine:

Step 1: Define the NPC:

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "MyAIController.h"
#include "MyNPC.generated.h"

UCLASS()
class MYGAME_API AMyNPC : public AActor
{
GENERATED_BODY()

public:
// Set up the OpenAI API key
UPROPERTY(EditAnywhere, Category = "NPC")
FString APIKey = "your_openai_api_key_here";

// Set up the AI Controller and conversation tree
AMyAIController* AIController;
TMap<FString, FString> ConversationTree;

// Set up the NPC's attributes
UPROPERTY(EditAnywhere, Category = "NPC")
FString Name;
UPROPERTY(EditAnywhere, Category = "NPC")
FString Backstory;
UPROPERTY(EditAnywhere, Category = "NPC")
FString Goals;
UPROPERTY(EditAnywhere, Category = "NPC")
FString Motivations;

// Set up the NPC's behavior and dialogue using OpenAI
UFUNCTION(BlueprintCallable, Category = "NPC")
void RespondToPlayer(FString PlayerMessage)
{
// Use OpenAI to generate a response to the player's message
FString Response = GenerateResponse(PlayerMessage);

// Have the NPC say the response
Say(Response);

// Use InWorld to move the NPC based on player interactions
if (Response.Contains("follow"))
{
MoveToPlayer();
}
else if (Response.Contains("stop"))
{
StopMoving();
}

// Use ElevenLabs to adjust the NPC's behavior based on game-specific conditions
if (Response.Contains("attack"))
{
AttackPlayer();
}
}

private:
FString GenerateResponse(FString Message)
{
// Use the OpenAI API to generate a response to the player's message
FString GeneratedResponse;

// Set up the OpenAI API parameters
FString Prompt = FString::Printf(TEXT("NPC says: %s\nPlayer says:"), *ConversationTree[GeneratedResponse]);
FString Engine = "text-davinci-002";
int32 MaxTokens = 100;
float Temperature = 0.5;
int32 TopP = 1;
int32 N = 1;
bool PresencePenalty = false;
bool FrequencyPenalty = false;
int32 Timeout = 5;

// Call the OpenAI API to generate a response
FOpenAICompletionResult CompletionResult = OpenAI_Completion(Prompt, Engine, APIKey, MaxTokens, Temperature, TopP, N, PresencePenalty, FrequencyPenalty, Timeout);

// Check if the response was successful
if (CompletionResult.Success)
{
GeneratedResponse = CompletionResult.Completions[0].Text;
}
else
{
GeneratedResponse = "I'm sorry, I didn't understand what you said.";
}

// Update the conversation tree with the new response
ConversationTree.Add(Message, GeneratedResponse);

return GeneratedResponse;
}

void Say(FString Message)
{
// Have the NPC say the message
UE_LOG(LogTemp, Warning, TEXT("%s: %s"), *Name, *Message);
}

// Move the NPC to the player's location using InWorld
void MoveToPlayer()
{
FVector PlayerLocation = GetPlayerLocation();
inworld.move(this, PlayerLocation);
}

// Stop the NPC's movement using InWorld
void StopMoving()
{
inworld
}

// Attack the player based on game-specific conditions using ElevenLabs
void AttackPlayer()
{
if (elevenlabs.can_attack())
{
elevenlabs.attack_player();
}
}

// Get the player's location using Unreal Engine
FVector GetPlayerLocation()
{
APawn* PlayerPawn = GetWorld()->GetFirstPlayerController()->GetPawn();
return PlayerPawn->GetActorLocation();
}

// Override the NPC's BeginPlay function to set up the AI Controller and conversation tree
virtual void BeginPlay() override
{
Super::BeginPlay();

// Set up the AI Controller
AIController = Cast<AMyAIController>(GetController());
if (AIController)
{
AIController->SetNPC(this);
}

// Set up the conversation tree
ConversationTree.Add("Hello", "Hi there, how can I help you?");
ConversationTree.Add("How are you?", "I'm doing well, thank you for asking. How are you?");
ConversationTree.Add("What's your name?", "My name is " + Name);
ConversationTree.Add("What's your backstory?", Backstory);
ConversationTree.Add("What are your goals?", Goals);
ConversationTree.Add("What motivates you?", Motivations);
}

// Override the NPC's Tick function to update the AI Controller's perception
virtual void Tick(float DeltaSeconds) override
{
Super::Tick(DeltaSeconds);

// Update the AI Controller's perception
AIController->UpdatePerception();
}
},

Step 2: Implement the NPC in Unreal Engine

  • Create a new blueprint class for the NPC
  • Add the NPC to your game world
  • Set up interactions between the NPC and players using triggers, events, or other game mechanics

Step 3: Configure the OpenAI API key

  • Replace `your_openai_api_key_here` in the script with your own OpenAI API key

Step 4: Configure InWorld and ElevenLabs

  • Install InWorld and ElevenLabs in your game environment
  • Set up the InWorld and ElevenLabs API keys in the script

Step 5: Test and refine

  • Test your NPC in the game environment to ensure that it behaves as expected
  • Tweak its behavior, dialogue, or decision-making based on player feedback or bugs that you encounter during testing

Conclusion

By following these steps and using OpenAI, Unreal Engine, InWorld, and ElevenLabs, you can create an NPC that interacts with real people in natural and engaging ways, enhancing the overall immersion and enjoyment of your game. With the power of AI and game development tools at your fingertips, the possibilities for creating unforgettable game experiences are endless.

--

--

Javier Calderon Jr

CTO, Tech Entrepreneur, Mad Scientist, that has a passion to Innovate Solutions that specializes in Web3, Artificial Intelligence, and Cyber Security