Build a Friendly NPC Medic in Fortnite
Tutorial beginner

Build a Friendly NPC Medic in Fortnite

Updated beginner

Build a Friendly NPC Medic in Fortnite

Imagine you are playing a tough battle. You are low on health. Suddenly, a friendly robot floats over and fixes your armor! That is what we will build today. We will make a custom NPC that heals other characters.

You do not need to be a math expert. You just need to know how to give instructions. Think of it like writing a recipe. The NPC is the chef. The code is the recipe. Let's get cooking!

What You'll Learn

  • How to create a special NPC script.
  • How to check if a character is hurt.
  • How to find the nearest friend to help.
  • How to heal them with a simple command.

How It Works

An NPC is just a computer-controlled character. It acts like a player. But we can change its brain. We use a tool called an NPC Behavior Script. This is a Verse file. It tells the NPC what to do.

Think of the script as a loop of thoughts. The NPC asks itself questions over and over.

  1. "Am I hurt?"
  2. "Is anyone else hurt?"
  3. "Who is closest to me?"
  4. "I will heal them!"

We call this a Finite State Machine. It is a fancy name for a set of rules. The NPC is in one "state" at a time. It might be "patrolling." Then it sees a hurt friend. It switches to "healing" state. Then it goes back to "patrolling."

We need two main tools. First, we need to scan for players. This means we look around. Second, we need to heal. This means we give health points. In Verse, we use built-in functions for this. We do not invent them. We use the ones Epic Games made for us.

Let's Build It

Open Unreal Editor for Fortnite (UEFN). Create a new NPC Behavior Script. Name it medic_example. Open it in Visual Studio Code.

Here is the code. Read the comments. They explain every line.

# This is our main script file.
# It tells the NPC how to behave.

using { /Fortnite.com/AI }
using { /Fortnite.com/Characters }
using { /Fortnite.com/Game }
using { /UnrealEngine.com/Temporary/Diagnostics }
using { /Verse.org/Simulation }

# We create a new type of NPC behavior.
# This is like a blueprint for our medic.
medic_behavior := class(npc_behavior):

    # This function runs when the game starts.
    OnBegin<override>()<suspends>: void =
        # We start a loop that never ends.
        # The NPC will keep thinking forever.
        loop:
            # Step 1: Find all players in the game.
            # GetPlayspace gives us the current match session.
            # GetPlayers() returns every player currently in the game.
            AllPlayers := GetPlayspace().GetPlayers()

            # Step 2: Filter only the hurt players.
            # We check their health. If it is low, we keep them.
            var HurtCharacters : []fort_character = array{}
            for (Player : AllPlayers):
                # Cast the player to a fort_character so we can read health.
                if (Character := Player.GetFortCharacter[]):
                    # IsPlayerHurt checks whether this character needs healing.
                    if (IsPlayerHurt[Character]):
                        set HurtCharacters += array{Character}

            # Step 3: Find the closest hurt player.
            # We pick the first one we find for now.
            if (HurtCharacters.Length > 0):
                if (Target := HurtCharacters[0]):
                    # Step 4: Heal the target!
                    # We use our helper function to add health.
                    # This makes them feel better.
                    HealCharacter(Target, 50.0)

            # Step 5: Wait a little bit.
            # We don't want to heal too fast.
            # It would be annoying!
            Sleep(2.0)

# This helper function checks if a player is hurt.
# fort_character exposes GetHealth() directly.
IsPlayerHurt(Character : fort_character)<decides><transacts>: void =
    # GetHealth() returns the current health as a float.
    # If health is less than 100, they are hurt.
    Character.GetHealth() < 100.0

# This helper function heals a character.
HealCharacter(Target : fort_character, Amount : float): void =
    # GetHealth() reads current health.
    # SetHealth() writes the new value.
    # Old health + Amount = New health.
    NewHealth : float = Target.GetHealth() + Amount
    Target.SetHealth(NewHealth)```

### Walkthrough

1.  **`OnBegin`**: This is the start button. When the NPC spawns, this code runs.
2.  **`loop`**: This is a **Loop**. It repeats the code inside. The NPC thinks, acts, thinks, acts.
3.  **`GetPlayspace().GetPlayers()`**: This gets a list of everyone in the game world.
4.  **`for` + `GetFortCharacter[]`**: This is a **Loop with a cast**. It looks at the list. It keeps only valid characters so we can read their health.
5.  **`HealCharacter`**: This is our custom function. It adds 50 health points.
6.  **`Sleep`**: This pauses the NPC. It prevents the NPC from healing instantly every frame.

## Try It Yourself

Now it is your turn to make changes. The NPC heals too fast! It heals every 2 seconds.

**Challenge:** Change the code so the NPC heals every **5 seconds** instead.

**Hint:** Look for the number `2.0` in the code. Change it to a bigger number. Save the file. Play your island. Watch the timer.

## Recap

You built a smart NPC. It checks for hurt players. It finds the closest one. It gives them health. You used a **Loop** to keep it thinking. You used a **Function** to do the healing. Great job! You made a helper for your island.

## References

- https://dev.epicgames.com/documentation/en-us/fortnite/create-your-own-npc-medic-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/en-us/uefn/create-your-own-npc-medic-in-verse
- https://dev.epicgames.com/documentation/en-us/fortnite/create-your-own-device-in-verse

Verse source files

Turn this into a guided course

Add Create your own NPC Medic 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