In the previous article about the NPC Spawner Device, we saw that the NPC Spawner uses an NPC Character Definition to obtain information about the NPC to be spawned.
Creating an NPC Character Definition allows a custom character to be saved as a project asset and reused in multiple places.
In this article, we will create three different NPC Character Definitions to give you an idea of the available options.
First, let's create a Verse class of the NPC Behavior type that we'll use in one of the NPC Character Definitions.
Open Verse Explorer, right-click on the project name, and select the option Add new Verse file to project.
Select the NPC Behavior template, enter npc_behavior_sample in the NPC Behavior Name field, and click Create.
This is an example NPC Behavior template. We won't modify the Verse code in this template.
Compile the Verse code using the Verse > Compile Verse Code option from the UEFN menu.
Now let's start creating the NPC Character Definitions. I recommend creating a folder to keep the NPC Character Definition assets.
In the Content Drawer, navigate to your project folder, right-click inside the Content Drawer, and select New Folder. Choose a name for the folder. In my project, I chose CharDefs.
Open the folder you created and right-click inside the Content Drawer. Select Artificial Intelligence > NPC Character Definition.
Repeat this step three times and rename the three assets with the following names:
- NPCCharDefGuard
- NPCCharDefWild
- NPCCharDefVerse
Double-click the NPCCharDefGuard asset to edit its properties. This NPC will be a Guard and will use the default behavior. You can keep the guard on Team Index 1 so that it won't attack the player. In Cosmetic Modifier, you can choose a Fortnite skin for the guard.
Double-click the NPCCharDefWild asset to edit it. This NPC will be a Wildlife type, so you need to choose a subtype to define the animal. In my example, I chose Chicken.
Double-click the NPCCharDefVerse asset. This NPC will be a Custom type and will use a Verse class to define its behavior. In Behavior, select Verse Behavior. In NPCBehavior Script, select the Verse class we created at the beginning of the article, npc_behavior_sample.
To demonstrate the use of NPC Character Definitions, let's create a Verse device that contains an array of NPC Spawner devices. Each element in the array will reference an NPC Spawner configured with a different NPC Character Definition.
The Verse device also has a Button device. When the player presses the button, one of the NPC Spawners in the array is randomly selected to spawn an NPC. This NPC can be a guard, an animal, or the custom NPC controlled by Verse code. We repeat this process using a loop.
In the Verse Explorer, right-click on the project name, and select the option Add new Verse file to project.
In Device Name put random_npc_spawners and click the Create Empty button.
Copy the Verse code below into the random_npc_spawners file:
using { /Fortnite.com/Devices }
using { /Verse.org/Simulation }
using { /Verse.org/Random }
using { /UnrealEngine.com/Temporary/Diagnostics }
random_npc_spawners := class(creative_device):
@editable
NpcSpawnerArray : []npc_spawner_device = array{}
@editable
Button : button_device = button_device{}
OnBegin<override>()<suspends>:void=
loop:
Button.InteractedWithEvent.Await()
if:
IndexSpawner: int = GetRandomInt(0, NpcSpawnerArray.Length-1)
NpcSpawner := NpcSpawnerArray[ IndexSpawner ]
then:
NpcSpawner.Spawn()
The Button.InteractedWithEvent.Await() expression suspends the execution of the OnBegin function until the player presses the button.
Next, a random index is generated and used to get a reference to one of the NPC Spawners in the array. Both expressions are failable, so we execute them as part of the if expression, which provides a failure context.
Save the file and compile the Verse code using the Verse > Compile Verse Code option from the UEFN menu.
Open the Content Drawer and add the new random npc spawners device to the level. We'll also need a Button Device.
When you drag an NPC Character Definition from the Content Drawer into the level, an NPC Spawner Device is created using that NPC Character Definition.
Drag the three NPC Character Definitions we created into the level.
The image below shows the devices we'll use.
Set Spawn Count to 5 for each NPC Spawner Device to allow each spawner to have multiple active NPCs.
Also uncheck Spawn On Timer so that the NPC is spawned only by the Verse code.
Select the random npc spawners in the level. In the Details panel, add three elements to NpcSpawnerArray and select the NPC Spawners in the level as their references. Also select the Button Device reference.
Save the level and click Launch Session to load it in Fortnite. Once in the game, press the button to spawn a new NPC of one of the three types.








