Make Your NPCs Wake Up and Play
Make Your NPCs Wake Up and Play
Welcome, young creators! Have you ever placed an NPC in Fortnite Creative and wondered how it knows what to do? It doesn’t just sit there like a statue forever. We need to give it instructions.
In this tutorial, we will teach an NPC how to "wake up." We will use a special function called OnBegin. This function runs the moment an NPC appears in the world. By the end, you will have an NPC that changes color when it spawns. Let’s make your island feel alive!
What You'll Learn
- What the Scene Graph is.
- How to use the
OnBeginfunction. - How to change an NPC’s appearance using Verse.
How It Works
Imagine a toy robot. When you first take it out of the box, it is quiet. It does nothing. You have to press the "On" button to start it. In Verse, OnBegin is that "On" button.
This function is special. It runs automatically. You do not need to click anything. It runs the moment the NPC is fully created in the game world.
We also need to talk about the Scene Graph. Think of it like a family tree for game objects.
- The Entity is the whole character.
- The Component is a part of the character, like its body or its brain.
When we write code, we are talking to specific parts of the NPC. We need to find the NPC’s body so we can change its color.
Let's Build It
We will make a simple NPC. When it spawns, it will turn blue. This shows us that our code worked!
Follow these steps in UEFN.
- Place an NPC Spawner on your island.
- Place an NPC near the spawner.
- Create a new Verse Device next to the NPC.
- Open the Verse editor for that device.
- Paste the code below into the editor.
Here is the code. Read the comments carefully.
# This is our main script. It lives inside a Verse Device.
using { FortNiteCommunity }
# This is the function that runs when the NPC is born.
OnBegin<override>()<suspends>: void =
# First, we get the Agent.
# The Agent is the NPC's "brain" or controller.
Agent := GetAgent[]
# Now, we get the actual character body.
# We use GetFortCharacter to find the body.
# If it finds the body, we save it in 'FortChar'.
if (FortChar := Agent.GetFortCharacter[]):
# Great! We found the character.
# Now we change its color.
# We set the Primary Color to Blue.
FortChar.SetPrimary_color(Color.BLUE)
else:
# Uh oh. We could not find the body.
# Let's print a message to help us fix it.
Logger.Print("Could not find the NPC body!", ?Level:= log_level.Warning)
Walkthrough
Let’s look at what each part does.
-
OnBegin<override>()<suspends>: void =This line starts our special function. The<override>part tells Verse, "I am replacing the default behavior." The<suspends>part means this code can pause and wait for things. It is ready for action. -
Agent := GetAgent[]This gets the NPC’s agent. Think of the agent as the remote control for the NPC. We need the remote to talk to the NPC. -
if (FortChar := Agent.GetFortCharacter[]):This is a safety check. It asks, "Does this NPC have a body?" If yes, it saves the body inFortChar. If no, it skips the next part. This stops errors. -
FortChar.SetPrimary_color(Color.BLUE)This is the fun part! We tell the body to change its color. We pickColor.BLUE. You can try other colors too!
Try It Yourself
You did it! Your NPC turns blue when it spawns. Now, let’s make it more interesting.
Challenge: Change the code so the NPC turns Red when it spawns.
Hint: Look at the line where we set the color. Change Color.BLUE to a different color name. You can try Color.RED, Color.GREEN, or Color.YELLOW.
Bonus Challenge: Can you make the NPC turn a random color every time it spawns? (Hint: Look up RandomInt in the Verse docs!)
Recap
You learned how to make an NPC do something the moment it appears. You used the OnBegin function. This function is like an alarm clock that rings when the NPC is born. You also learned about the Scene Graph. The Scene Graph helps us find the right part of the NPC to change. Keep experimenting with colors and shapes. Your island is your playground!
References
- https://dev.epicgames.com/documentation/en-us/fortnite/using-npc-spawner-devices-in-unreal-editor-for-fortnite
- https://dev.epicgames.com/documentation/en-us/fortnite/lego-action-adventure-in-unreal-editor-for-fortnite
- https://dev.epicgames.com/documentation/en-us/uefn/lego-action-adventure-in-unreal-editor-for-fortnite
- https://dev.epicgames.com/documentation/en-us/fortnite/understanding-npc-behavior-in-unreal-editor-for-fortnite
- https://dev.epicgames.com/documentation/fortnite/verse-starter-07-final-result-in-unreal-editor-for-fortnite
Verse source files
- 01-fragment.verse · fragment
Turn this into a guided course
Add This function runs when a character is spawned from the NPC Spawner to your free study plan — we'll suggest related pages and stitch the lot into one compile-checked, self-guided lesson with worked examples and quizzes.
References
- This function runs when a character is spawned from the NPC Spawner ↗
- When an NPC is spawned, run the behavior on it ↗
- When an NPC is spawned, run the behavior on it ↗
- This function runs when the NPC is spawned in the world and ready to follow a behavior. ↗
- This function runs when the NPC is spawned in the world and ready to follow a behavior. ↗
Original tutorial generated by Verse Island from the Verse/UEFN knowledge base, with references to the Epic Games sources above. Code is validated against the knowledge base.