Code the CTF: Build Your Own Capture the Flag in UEFN
Tutorial beginner

Code the CTF: Build Your Own Capture the Flag in UEFN

Updated beginner

Code the CTF: Build Your Own Capture the Flag in UEFN

Stop playing other people’s Capture the Flag islands where the enemy base is just a cardboard box behind a tree. It’s time to build your own arena, complete with logic that tracks who stole the flag, who got eliminated, and who actually deserves the victory screen.

In this tutorial, we’re going to build a functional Capture the Flag (CTF) match. We’ll use Verse, Epic’s programming language, to create the "brain" of the operation. You don’t need to be a math wizard; you just need to understand that a computer is basically a really fast, really obedient minion who needs very specific instructions.

What You'll Learn

  • Variables: How to track the score (like a scoreboard that updates in real-time).
  • Events: How to make the game react when a player touches a flag (like a tripwire).
  • Logic: How to decide who wins based on simple rules.
  • Scene Graph Basics: Understanding where your code lives in relation to your game objects.

How It Works

In Fortnite Creative, devices (like Capture Areas or Item Granters) handle the visual and input parts of the game. But they don’t "think." They don’t know if Blue has won or if Red is cheating. That’s where Verse comes in.

Think of Verse as the Game Director sitting in the control booth. The devices are the stagehands pulling levers. The Game Director watches what the stagehands do and announces the winner.

Here is the core loop of our CTF logic:

  1. The Setup: We need two flags (Red and Blue) and two capture zones.
  2. The Trigger: When a player enters a capture zone holding the enemy flag, something happens.
  3. The State Change: We need a Variable (a storage box) to remember how many points Blue has. If Blue captures, we add 1 to Blue’s box.
  4. The Win Condition: We check if Blue’s box has reached 3. If yes, we trigger the "Blue Wins" screen.

The Scene Graph: Where Does the Code Live?

In Unreal Engine 6 (and Verse), everything is part of the Scene Graph. Imagine a giant family tree.

  • The Island is the parent.
  • The Game Mode (our Verse script) is a child of the Island.
  • The Players, Flags, and Capture Areas are siblings or cousins in that tree.

Verse scripts live in a special object called a GameScript. This script is always present in the game world, watching over everything else. It doesn’t move, it doesn’t shoot, but it knows everything.

Let's Build It

We aren’t just writing code; we’re wiring a logic trap. We’ll create a simple Verse script that listens for flag captures and updates the score.

Step 1: The Setup in UEFN

  1. Open Unreal Editor for Fortnite (UEFN) and create a Blank project.
  2. In the Outliner (the list of all objects on the left), find IslandSettings. Select it.
  3. In the Details Panel (right side), look for Game Mode. We will attach our Verse script here later. For now, just know that this is the "command center" for our island.

Step 2: The Verse Script

We need a script that tracks scores. In Verse, we use a Class to define our script. Think of a Class as a blueprint for a specific type of object. Our blueprint is a CaptureFlagGameScript.

Copy this code into a new Verse file (right-click in the Content Browser > New > Verse File) and name it CaptureFlagLogic.

// This is our "Blueprint" for the game logic.
// It inherits from GameScript, which means it lives in the world and watches everything.
script class CaptureFlagGameScript is GameScript:
    
    #region Variables (The Scoreboards)
    // A variable is like a loot box that holds a value.
    // We use 'int' (integer) for whole numbers.
    // These start at 0.
    Blue_Score: int = 0
    Red_Score: int = 0
    
    // A constant is a rule that never changes.
    // The win condition is always 3 points.
    const WIN_SCORE: int = 3
    #endregion

    #region Events (The Triggers)
    // An event is a signal that says "Something happened!"
    // This function runs automatically when the game starts.
    event OnBegin():
        // When the match starts, let's announce it.
        Print("Match Started! First to 3 captures wins.")
    #endregion

    #region Logic (The Brain)
    // This is a custom function we will call manually when a capture happens.
    // 'team' is a label (like "Blue" or "Red").
    function OnFlagCaptured(team: string):
        // We use an 'if' statement. It's like a decision tree.
        // If the team is Blue, add 1 to Blue's score.
        if (team == "Blue"):
            Blue_Score += 1
            Print("Blue captured the flag! Score: ", Blue_Score)
            
            // Check if Blue won
            if (Blue_Score >= WIN_SCORE):
                Print("BLUE WINS THE MATCH!")
                # Note: In a full game, you'd trigger the Win Screen device here.
        
        # If the team is Red, add 1 to Red's score.
        else if (team == "Red"):
            Red_Score += 1
            Print("Red captured the flag! Score: ", Red_Score)
            
            # Check if Red won
            if (Red_Score >= WIN_SCORE):
                Print("RED WINS THE MATCH!")
    #endregion

Step 3: Connecting the Dots

Code doesn’t do anything until it’s told to run. We need to connect the Capture Area devices to our script.

  1. Place two Capture Area devices in your map. One for Blue, one for Red.
  2. Place a Capture Item Spawner for each team’s flag.
  3. Go back to your CaptureFlagGameScript in UEFN. You need to make the script "aware" of the capture events. Note: In a full production build, you would use Verse events linked to the Capture Area's OnPlayerEnter signal. For this beginner tutorial, we will simulate the logic flow.

To make this work in the editor, you typically wire the Capture Area's output to a Logic device or use Verse to listen to the OnPlayerEnter event of the Capture Area. However, since Verse is object-oriented, the cleanest way is to have the Capture Area trigger a function in our Game Script.

For the sake of this tutorial, imagine that when a player enters the Blue Capture Area, it shouts "Hey Script! Blue just scored!" and our OnFlagCaptured("Blue") function runs.

Step 4: Testing

  1. Press Play in UEFN.
  2. Walk into the Capture Area.
  3. Watch the Output Log (bottom of the screen) or the in-game HUD. If you’ve wired it correctly, you should see "Blue captured the flag!" printed out.

Try It Yourself

You’ve got the basic scoring logic. Now, make it harder.

Challenge: Add a "Reset" feature. If a player is eliminated, we want to reset the flag to its base. But wait, what if we want to reset the entire score if someone types a specific command?

  1. Add a new variable called Match_Over: bool = false. (A bool is just a light switch: On/True or Off/False).
  2. Modify your OnFlagCaptured function. If Match_Over is True, ignore any new captures.
  3. Hint: You’ll need to check the Match_Over variable at the very top of your function. If it’s true, just return (stop the function) immediately.

Recap

  • Variables store changing data (like scores).
  • Constants store fixed rules (like the win condition).
  • Functions are reusable blocks of logic (like "Check for Win").
  • Events are triggers that start the code running.
  • Verse is the language that ties your devices together into a playable game.

You now have the foundational logic for a CTF mode. The next step is to wire up the actual Visual Devices (HUDs, Win Screens) to these Verse functions. But that’s a battle for another tutorial. For now, go steal some flags.

References

  • https://dev.epicgames.com/documentation/en-us/fortnite/build-a-capture-the-flag-in-unreal-editor-for-fortnite
  • https://dev.epicgames.com/documentation/en-us/uefn/build-a-capture-the-flag-in-unreal-editor-for-fortnite
  • https://dev.epicgames.com/documentation/en-us/fortnite/capture-the-flag-1-set-up-the-game-in-unreal-editor-for-fortnite
  • https://dev.epicgames.com/documentation/en-us/fortnite/capture-the-flag-5-set-up-flag-mechanics-in-unreal-editor-for-fortnite
  • https://dev.epicgames.com/documentation/en-us/fortnite/capture-the-flag-2-create-the-island-in-unreal-editor-for-fortnite

Verse source files

Turn this into a guided course

Add build-a-capture-the-flag-in-unreal-editor-for-fortnite 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