Make Your Own Friendly Guard NPC
Tutorial beginner

Make Your Own Friendly Guard NPC

Updated beginner

Make Your Own Friendly Guard NPC

Imagine an NPC that waves hello when you get close. Or one that follows you like a puppy. In Fortnite islands, NPCs usually just stand there or fight. But you can change that! You can give them their own personality.

We will build a simple "Friendly Guard." This NPC will stop running away and wave at you when you walk near it. You will learn how to write the "brain" code for a character. Let’s make your island feel alive!

What You'll Learn

  • What an NPC Behavior script is.
  • How to use Events to react to the player.
  • How to make an NPC move or act using Verse.
  • How to link your code to a device in UEFN.

How It Works

Think of an NPC like a robot toy. The robot has batteries (the game engine). It has wheels (movement). But it needs a remote control to know what to do.

In UEFN, that remote control is a Verse Script. Specifically, we use an NPC Behavior script. This is a special file that tells the NPC what to think.

Here is the big idea: Events. An event is like a doorbell. When the doorbell rings, you know someone is there. In our game, the "doorbell" is the player getting close. The "answer" is the NPC stopping and waving.

We will use the OnBegin event. This happens when the NPC first appears on the island. We will set up a loop. The loop is like a heartbeat. It keeps checking: "Is the player close?" If yes, act friendly! If no, go back to normal.

We will use the /Fortnite.com/AI module. This is a toolbox. It has tools for talking to other characters and moving around.

Let's Build It

We will create a script that makes an NPC follow you slowly. This is easier than waving and shows how movement works.

Step 1: Create the Script

  1. Open Verse Explorer in UEFN.
  2. Click New Verse File.
  3. Name it FriendlyGuardBehavior.
  4. Paste this code inside.
using { /Fortnite.com/AI }

# This is our NPC Behavior class.
# It inherits from npc_behavior.
# This means our script has all the AI tools.
FriendlyGuardBehavior <: npc_behavior:

    # This function runs when the NPC spawns.
    OnBegin (): <override> void:
        # We start a loop to check for the player.
        # This loop runs forever until the NPC dies.
        loop:
            # Get the closest player to this NPC.
            # We use GetClosestPlayer to find who is near.
            player := GetClosestPlayer ()
            
            # Check if the player is within 500 units.
            # 500 units is like walking 5 steps.
            if player != None:
                distance := GetDistance (player)
                if distance < 500:
                    # If close, move towards the player.
                    # We use MoveTo to tell the NPC where to go.
                    MoveTo (player)
                    # Wait a tiny bit so it doesn't glitch.
                    Wait (0.1)
            
            # Wait a short time before checking again.
            # This saves battery (CPU power).
            Wait (0.2)

Step 2: Understand the Code

Let’s look at the important parts.

using { /Fortnite.com/AI } This line brings in the AI toolbox. Without this, your script cannot talk to the game’s character system. It is like needing a key to open a locked door.

FriendlyGuardBehavior <: npc_behavior: This defines our class. npc_behavior is the parent class. It gives us built-in functions like MoveTo and GetClosestPlayer. We are saying, "I want my behavior to be based on the standard NPC rules."

OnBegin (): <override> void: This is an Event. It triggers when the NPC is created. We override it to add our own custom actions. Think of it as setting up the robot before it starts working.

loop: A loop repeats code. Here, it checks for players over and over. We add Wait commands inside. This prevents the game from freezing. If you don’t wait, the computer tries to check millions of times per second. It will crash!

GetClosestPlayer () This function looks around the NPC. It finds the nearest human player. If no one is there, it returns None. That is why we check if player != None.

MoveTo (player) This tells the NPC’s pathfinding system to walk toward the player’s current location. The NPC will take the smartest route, avoiding walls.

Step 3: Connect to Your Island

  1. Place an NPC Spawner device on your island.
  2. Choose a character type (like a Husk or a Custom Character).
  3. In the Details Panel, find the Behavior field.
  4. Select your FriendlyGuardBehavior script from the list.
  5. Press Play.

Walk near the NPC. Watch it turn and follow you!

Try It Yourself

You made a follower! Now let’s make it smarter.

Challenge: Make the NPC stop following you if you run away.

Hint: Use an else statement inside your if distance < 500 block. If the player is farther than 500 units, maybe the NPC should just Wait instead of moving. Or you could make it move to a specific spot instead of the player.

Recap

  • NPC Behaviors are Verse scripts that control character actions.
  • Events like OnBegin let you start code when an NPC spawns.
  • Loops with Wait let you check for players repeatedly without crashing.
  • AI Functions like GetClosestPlayer and MoveTo handle the hard work of finding and walking to targets.

You just gave an NPC a mind of its own! Great job, developer!

References

  • https://dev.epicgames.com/documentation/en-us/uefn/understanding-npc-behaviors-in-unreal-editor-for-fortnite
  • https://dev.epicgames.com/documentation/en-us/fortnite/create-custom-npc-behavior-in-unreal-editor-for-fortnite
  • https://dev.epicgames.com/documentation/en-us/uefn/create-custom-npc-behavior-in-unreal-editor-for-fortnite
  • https://dev.epicgames.com/documentation/fortnite/verse-api/fortnitedotcom/ai/npc_behavior
  • https://dev.epicgames.com/documentation/en-us/uefn/verse-starter-template-1-creating-npc-behavior-in-unreal-editor-for-fortnite

Verse source files

Turn this into a guided course

Add NPC Character Behavior 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.

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.

Comments

    Sign in to vote, comment, or suggest an edit. Sign in