The "Get Back Here!" Button: Creating AI Leashes in Verse
Unverified — this example may not compile as-is.
The Verse code below did not pass our automated compile check, so it isn't marked verified and is hidden from the guide listing. The explanation may still be useful, but treat the code as a draft and adapt it before use.
The "Get Back Here!" Button: Creating AI Leashes in Verse
You know that feeling when you spawn a Turret, and it just sits there like a confused lawn ornament until you shoot it? Or when an NPC guard wanders off the edge of the map because they forgot their own name? It’s annoying. It’s unprofessional. It’s not what you want for your island.
In this tutorial, we’re going to fix that. We’re building an AI Leash System. Think of a leash like the storm circle, but instead of shrinking, it’s an invisible bubble around an objective (or a player) that says, "You are allowed to patrol in here, but if you step out, you get pulled back." We’ll use Verse to tie our guards to specific spots so they stay on duty, not on vacation.
What You'll Learn
- The Scene Graph & Hierarchy: How devices talk to each other without getting lost in the code.
- Interfaces (
fort_leashable): How to give an NPC the "leashable" trait (like giving a player a shield). - The Leash Position Device: The physical marker in the editor that defines the guard’s playground.
- Basic Logic: How to tell a guard, "Stay near this point, or I’m pulling you back."
How It Works
Before we write a single line of code, let’s look at the mechanics.
1. The Leash Position (The Anchor)
In Fortnite Editor, you place a Leash Position device. This is your anchor point. It’s like placing a flag in the sand. The guard doesn’t know where the flag is yet; we have to tell them.
2. The Guard (The Leashed Entity)
Not every NPC can be leashed. Only Guard-type NPCs have the fort_leashable interface. Think of an Interface like a specific item type in your inventory. If you have a "Shield Potion" item, you can drink it. If you have a "Guard" NPC, they have the fort_leashable interface, which means they can be tied to a leash. Other NPCs (like Wildlife or Custom characters) don’t have this item in their inventory, so they can’t be leashed.
3. The Connection (The Verse Script)
This is where the magic happens. We need a script that:
- Finds the Guard.
- Finds the Leash Position.
- Tells the Guard: "Your leash is tied to this position. Radius: 10 meters."
If the guard tries to walk 11 meters away, the leash snaps them back. It’s like the storm pulling you in, but much more polite.
Let's Build It
We’re going to set up a simple scenario: A guard patrols around a chest. If you try to loot the chest, the guard stays right next to it, ready to defend.
Step 1: Place Your Devices
- Open Unreal Editor for Fortnite (UEFN).
- Place a Guard NPC (e.g., a Minigunner or a standard Guard).
- Place a Chest nearby.
- In the Verse Explorer, ensure you have a Leash Position device placed exactly where the chest is. You can use a dummy prop to visualize this spot if needed.
Step 2: The Verse Script
Create a new Verse file. Let’s call it GuardLeash.v.
Here is the code. Don’t panic at the syntax; we’ll break it down line by line.
# Import the necessary Verse modules for devices and NPCs
using /Fortnite.com/Devices
using /Verse.org/Simulation
using /Fortnite.com
# This is our main script class. It's like the "Game Mode" for this specific interaction.
script GuardLeashScript() implements IInitializable:
# VARIABLES: These are our "inventory slots" for references.
# We don't know the values yet, just that we need slots for them.
Guard := struct{}
LeashPosition := struct{}
# INITIALIZE: This function runs ONCE when the game starts.
# Think of this as the "Loadout Screen" before the match begins.
Initialize(): void = block:
# 1. Get the Guard NPC from the editor.
# We assume you placed a device named "MyGuard" in the level.
# If you didn't name it that, change "MyGuard" to your device's name.
Guard := World.GetDevice<GuardDevice>("MyGuard")
# 2. Get the Leash Position device.
# We assume you placed a device named "GuardLeashPoint".
LeashPosition := World.GetDevice<LeashPositionDevice>("GuardLeashPoint")
# 3. Get the Leash Interface from the Guard.
# This is like checking if the Guard has the "Leashable" trait.
# If the Guard isn't a Guard-type NPC, this might fail or return empty.
Leashable := Guard.GetFortLeashable[]
# 4. Apply the Leash!
# We tell the Leashable interface to tie the Guard to the LeashPosition.
# The second argument (15.0) is the radius in meters.
# The guard will stay within 15 meters of the LeashPosition.
Leashable.SetLeash(LeashPosition, 15.0)
Walkthrough: What Just Happened?
-
script GuardLeashScript() implements IInitializable:This defines our script.IInitializablemeans "I have anInitializefunction that runs at the start." It’s like saying, "I’m ready for the match, here’s my pre-game setup." -
Guard := World.GetDevice<GuardDevice>("MyGuard")This is how we find our NPC. In Fortnite, you name your devices in the editor. Verse looks for the device named"MyGuard"and puts a reference to it in ourGuardvariable. If you don’t name your device "MyGuard," this line will break. It’s like trying to find a friend in a crowd by shouting the wrong name. -
Leashable := Guard.GetFortLeashable[]This is the critical step. We’re asking the Guard, "Do you have thefort_leashableinterface?" If yes, we get access to it. If no (e.g., you tried to leash a Boar), this returns nothing. -
Leashable.SetLeash(LeashPosition, 15.0)This is the command. We’re calling theSetLeashmethod on the interface. We pass it the anchor (LeashPosition) and the radius (15.0). Now, the Guard’s AI knows its boundaries.
Step 3: Hook It Up
- In UEFN, place a Verse Device in your level.
- In the Verse Device’s properties, set the Script to
GuardLeashScript. - Make sure your Guard NPC is named MyGuard and your Leash Position is named GuardLeashPoint (or update the code to match your names).
- Play the game. Watch your guard. They’ll stay within 15 meters of the chest. If you push them, they’ll slide back. If they try to wander off, they’ll turn around.
Try It Yourself
Now that you have the basics, try these challenges:
- Multiple Guards: Can you leash two guards to the same Leash Position? What happens if you have two different Leash Positions?
- Dynamic Leashes: What if you want the guard to follow you? Can you change the
SetLeashtarget to the Player’s device instead of a static Leash Position? (Hint: You’ll need to get the Player’s device reference). - Radius Control: Change the radius to
5.0and then50.0. How does it change the guard’s behavior? Does a larger radius make them feel more "lazy"?
Hint for Challenge 2: You’ll need to use World.GetPlayerDevice(0) to get the first player, then use that player’s location or device as the target. But remember, players don’t have fort_leashable! So you’ll need to leash the Guard to the Player’s location, not the Player itself. Check the Verse docs for GetLocation().
Recap
- Leashes keep NPCs in a specific area, preventing them from wandering off.
- Only Guard-type NPCs have the
fort_leashableinterface, which allows them to be leashed. - You use Verse to connect a Guard to a Leash Position device via the
SetLeashmethod. - Always name your devices in the editor so Verse can find them by name.
Now go forth and leash those guards. Let them guard, not wander.
References
- https://dev.epicgames.com/documentation/en-us/fortnite/stronghold-04-set-up-leash-devices-in-unreal-editor-for-fortnite
- https://dev.epicgames.com/documentation/en-us/uefn/verse-stronghold-template-2-add-leashes-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/fortnite/create-custom-npc-behavior-in-unreal-editor-for-fortnite
- https://dev.epicgames.com/documentation/en-us/fortnite/stronghold-03-add-verse-script-to-devices-in-unreal-editor-for-fortnite
Verse source files
- 01-standalone.verse · standalone
Turn this into a guided course
Add fortnite-20-guards-following-player-using-leash 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
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.