Prerequisites
Now that you have created your first persona and refined its personality through interacting with it in the Prompt Editor, you can add your NPC to a conversation. To converse with an NPC in game, you must:
Define a new
npc_behaviorin Verse.Add your
npc_behaviorto your NPC Character Definition.Add your NPC Character Definition to an NPC Spawner.
Ensure you have Voice Chat Enabled as Voice Chat is required to converse with persona NPCs.
Create an Empty NPC Behavior
To set up a custom NPC Behavior in Verse, follow these steps:
In the Menu Bar, select Verse > Verse Explorer.
In the Verse Explorer, right-click on your project name and choose Add new Verse file to project to open the Create Verse Script window.
In the Create Verse Script window, click NPC Behavior Basic to select it as your template.
Name the NPC Behavior persona_npc_behavior under Configure.
Select Create to create your Verse file.
If you select NPC Behavior instead of NPC Behavior Basic and choose Create for your persona_npc_behavior, the created Verse file contains a predefined npc_behavior with existing functionality that you must replace with behaviors explained here. This tutorial provides you with a full npc_behavior Verse script you can use, so you are safe to choose NPC Behavior Basic with Create or Create Empty since the script provides everything you need.
The persona_npc_behavior is where you can define and add a persona_component to your NPC through Verse. All NPCs spawned by NPC Spawners are agents and have an associated entity. This associated entity is how you add Scene Graph Components, such as the persona_component to an NPC agent.
The persona_component is added automatically when a Persona Modifier is added to an NPC.
NPC Behaviors are defined in Verse inheriting from the base npc_behavior class. For more information about NPC Behaviors, see the Understanding NPC Behaviors page.
Edit your Persona NPC Behavior
Verse is required to add your NPC persona as a conversation target for the player. Without adding your NPC as a conversation target for your player, you cannot converse with your NPC persona.
In the following sections, the Verse script defines the persona_npc_behavior . To add this Verse code to your empty persona_npc_behavior, follow these steps:
In the Menu Bar, select Verse > Open Project in VS Code…
Once VS Code opens, find your
persona_npc_behavior.versefile in the VS Code Explorer.Copy and paste the content of
persona_npc_behavior.versebelow into your Verse file in your project.Compile your Verse code either in the UEFN Toolbar or within VS Code.
You can also use Verse to create experiences where a team of players interact with LLM agents. Verse handles the interactions between multiple players and the LLM agents using CreateAudioTeamAndAddPlayers(), AddMember(), and AddChatChannel().
For a step-by-step walkthrough of the Verse script, follow the steps under Explanation of Script.
persona_npc_behavior.verse
using { /Fortnite.com/AI }
using { /Fortnite.com/Devices }
using { /Fortnite.com/Playspaces }
using { /UnrealEngine.com/Conversations }
using { /UnrealEngine.com/Temporary/Diagnostics }
using { /Verse.org/Chat }
using { /Verse.org/SceneGraph }
using { /Verse.org/Simulation }
Explanation of Script
The
TeamNameis the name for the voice channel being created.TheOptionAgentGroupholds the group of agents going into the voice channel (TeamName). TheOptionVoiceChannelis the variable of the actual Voice Channel that the players and NPC are in.Verse#Team name for the Team Voice Channel we are creating. TeamName<localizes>:message = "Persona Chat" #Variable that stores the agent group we are using to create the voice channel. We can use this group to create new channels or get current players in a conversation. var OptionAgentGroup:?agent_group(team_member_info) = false #Variable that stores the Voice Channel that our players and NPC will be inside of. This is what actually holds the conversation. var OptionVoiceChannel:?voice_channel(team_member_info) = falseAddPlayersAsConversationTargetsets up a live voice conversation between the NPC and every player in its playspace. The function first gathers everything it needs, and only if all of those succeed does it build the agent group and voice channel, wire the players and NPC into them, and register the players as conversation targets.First it creates a new
agent_grouptyped withteam_member_info, which holds everyone in the conversation. It then creates a voice_channel (also typed withteam_member_info), naming it withTeamNameand pointing its Group at the agent group just created. This channel is what carries the conversation audio.Next it populates the group: it loops over every player in the playspace and adds each as a member, then adds the NPC agent as well, so all participants share one group.
It then calls
SimEntity.AddChatChannel(TeamVoiceChannel)to register the voice channel with the Audio Chat system, which brings the channel online. Finally it loops over the players again, callingSetConversationTargeton each so they're pointed at the NPC'spersona_componentthrough that channel.The last step stores the group and channel in the
OptionAgentGroupandOptionVoiceChannelvariables (wrapped inoption{}), so later helper functions can reference or modify the conversation without rebuilding it.Verse# Add the Player as a conversation target for this NPC AddPlayersAsConversationTarget():void= if: NPCEntity := GetEntity[] Playspace := NPCEntity.GetPlayspaceForEntity[] NPCAgent := GetAgent[] Players := NPCEntity.GetPlayspaceForEntity[].GetPlayers() SimEntity := Players[0].GetSimulationEntity[] PersonaComponent := NPCEntity.GetComponent[persona_component]RemovePlayerAsConversationTargetremoves a single player from the NPC's conversation. It takes one player argument and callsClearConversationTarget(), which detaches that player from whatever conversation target they were pointed at (The inverse of theSetConversationTargetcall made inAddPlayersAsConversationTarget).Verse# Remove Player as a conversation target RemovePlayerAsConversationTarget(Player:player):void= Player.ClearConversationTarget() Logger.Print("Removed from NPC conversation", ?Level := log_level.Debug)OnBeginadds the structured output to the session and sets the player as the conversation target. In a game where players can join late, you want to ensure you add them on thePlayerAddedEventor when a player walks up to an NPC via a trigger.VerseOnBegin<override>()<suspends>:void= AddPlayersAsConversationTarget()In
OnEndtheConversationis cleaned and removed from all players. This is good practice.VerseOnEnd<override>():void= for (Player : GetEntity[].GetPlayspaceForEntity[].GetPlayers()): RemovePlayerAsConversationTarget(Player)
Add NPC Behavior to NPC Character Definition
To add your persona_npc_behavior to your NPC Character Definition, follow these steps:
Navigate to your PersonaNPCDefinition in the Content Drawer.
Select the PersonaNPCDefinition to open for editing.
Under NPC Character Behavior, select Verse Behavior > persona_npc_behavior.
Ensure you save your changes to your NPC Character Definition before closing the tab.
Once your NPC Character Definition is added to an NPC Spawner, your NPC persona is automatically added as a conversation target for your player.
Add NPC Spawner with NPC Character Definition
To spawn an NPC with your customized persona, you must add an NPC Spawner to your level and add your PersonaNPCDefinition. To add an NPC Spawner Device, drag your PersonaNPCDefinition from the Content Drawer into the Viewport.
Converse with your NPC
To converse with your NPC, launch a session and start the game. Press the key or button on your input device indicated on-screen and converse with your NPC!