The Magic Map: Finding Items by Name in Verse
Tutorialbeginner—

The Magic Map: Finding Items by Name in Verse

Updated beginner

The Magic Map: Finding Items by Name in Verse

Welcome, creator! Have you ever played a game where you need to find a specific item? Maybe you need to find the "Key" to open a door, or you want to know which team has the most points.

In programming, we often need to look things up quickly. We don't want to search through a whole list one by one. That takes too long! We want to say, "Give me the thing for 'Apple'."

Today, we will learn about Maps. A map is like a giant, magical dictionary. You give it a word (the key), and it instantly shows you the answer (the value). We will use this to build a simple "Team Scoreboard" for our Fortnite island.

What You'll Learn

  • What a Map is and why it is faster than a list.
  • How to store data using Key-Value Pairs.
  • How to look up a player’s score using their name.
  • How to update scores when a player gets an elimination.

How It Works

<!-- section-art:how-it-works --> The Magic Map: Finding Items by Name in Verse: How It Works

Instant Key Lookup

Imagine you have a big box of toys. If you want to find your red car, you might have to dig through the whole box. That is slow!

But what if you had a special shelf? You put the red car on the shelf labeled "Red." When you want the red car, you just look at the "Red" label. You get it instantly!

In Verse, a Map works just like that shelf.

Key-Value Pairs

A map stores two things together:

  1. The Key: This is the label. It is unique. You can only have one "Red" label.
  2. The Value: This is the toy inside. It can be anything (a number, a color, a player).

We write a map like this: MyMap["Label"] = Toy

If we want to know the toy for "Red," we ask: MyMap["Red"]

The computer looks at the label "Red" and hands you the toy. No digging required!

Why Use a Map for Scores?

In a game, we might have many players. We want to track their scores.

  • Key: The Player (or their name).
  • Value: The Score (a number).

If Player A gets a point, we just update their score in the map. We don't need to find them in a long list. We just look up their name in the map and add one.

Let's Build It

<!-- section-art:let-s-build-it --> The Magic Map: Finding Items by Name in Verse: Let's Build It

Score Map

We will build a simple system. When a player hits a target, we will give them points. We will use a map to remember how many points each player has.

Step 1: The Setup

First, create a new Verse file in UEFN. We will create a Device. This device will listen for hits.

Step 2: The Code

Here is the code for our Score Tracker. Read the comments carefully!

# This is our main device class.
# It acts like the brain of our scoreboard.
class ScoreTrackerDevice is WorldDevice():
    # This is our Map!
    # It maps a Player (the Key) to an Integer (the Value).
    # We call it PlayerScores.
    PlayerScores : [Player]int = map{}

    # This function runs when the game starts.
    OnBegin<override>()<suspends>: void =
        # We start with an empty map.
        # It is ready to store scores!
        Print("Scoreboard is ready!")

    # This function runs when a hit happens.
    # We will connect a Trigger to this later.
    HandleHit(Attacker: Player, Target: Entity)<suspends>: void =
        # Check if the attacker is actually a player.
        # If they are not a player, we stop here.
        if (Attacker == None):
            return

        # Look up the current score for this player.
        # If the player is not in the map yet,
        # the default value for int is 0.
        CurrentScore : int = PlayerScores[Attacker]

        # Add 1 to their score.
        NewScore : int = CurrentScore + 1

        # Update the map with the new score.
        # We use the player as the Key.
        PlayerScores[Attacker] = NewScore

        # Tell everyone what happened!
        Print("{Attacker.Name} now has {NewScore} points!")

Walkthrough

  1. PlayerScores : [Player]int = map{}: This line creates our map.

    • [Player]int means the Key is a Player and the Value is an int (number).
    • map{} creates an empty map. It is like an empty shelf.
  2. PlayerScores[Attacker]: This looks up the score.

    • We use square brackets [].
    • We put the Attacker (the player) inside.
    • If the player is new, the map returns 0. This is safe!
  3. PlayerScores[Attacker] = NewScore: This updates the score.

    • We take the old score, add 1, and put it back in the map.
    • The map remembers this new number forever (until the game restarts).

Try It Yourself

Now it is your turn to make this work in Fortnite!

Challenge: Connect a Trigger Volume to this device.

  1. Place a Trigger Volume in your island.
  2. Make it a "Player Only" trigger.
  3. Link the Trigger's OnBeginOverlap event to your HandleHit function.
  4. Play the game! Walk into the trigger. Watch the chat log for your score.

Hint: Make sure your device is placed in the world. If you don't see the scores updating, check if the Trigger is actually sending the event to the device.

Bonus Challenge: Can you add a second map to track "Team Colors"?

  • Create a new map: TeamColors : [Player]string.
  • Set the color when a player joins.
  • Look up the color when they score.

Recap

You did it! You learned how to use a Map in Verse.

  • A Map stores Key-Value Pairs.
  • The Key is the unique label (like a player's name).
  • The Value is the data (like a score).
  • You can look up and update data instantly using square brackets [].

Maps are superpowers for your code. They help you keep track of many things at once. Keep building, and have fun!

References

  • https://dev.epicgames.com/documentation/en-us/uefn/verse-language-quick-reference
  • https://dev.epicgames.com/documentation/en-us/fortnite/map-in-verse
  • https://dev.epicgames.com/documentation/en-us/fortnite/team-elimination-4-tracking-players-using-maps-in-verse
  • https://dev.epicgames.com/documentation/en-us/fortnite/import-from-fab-in-unreal-editor-for-fortnite
  • https://dev.epicgames.com/documentation/en-us/fortnite/gameplay-tags-in-verse

Verse source files

Check your understanding

Test yourself with an interactive quiz and track your progress + earn XP — free for members.

Turn this into a guided course

Add Verse maps to look things up by a key 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