Keep Score with Player Maps
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.
Keep Score with Player Maps
Imagine you are keeping score in a video game. You need to know who scored a point. A map is like a special scoreboard. It links a player to their score. You will learn how to build this scoreboard in Verse.
What You'll Learn
- What a map is in programming.
- How to link a player to a number.
- How to update a score when a player wins.
How It Works
Think of a map like a real-world address book. You have a name. You have an address. The name is the key. The address is the value.
In Verse, we use maps to track things. We want to track eliminations. An elimination is when you defeat another player.
Here is the plan.
- We create a map.
- The key is the player.
- The value is the score.
- When a player scores, we update their value.
This is like a digital sticky note. You stick it on the player. It shows their current score.
Let's Build It
We will write code for a creative device. This device tracks scores.
Copy this code into your Verse editor. Read the comments. They explain each line.
using { /Fortnite.com/Characters }
using { /Fortnite.com/Game }
# This is a map type.
# It links a player to an integer (a whole number).
# Think of it as a list of Player -> Score.
PlayerScoreMap := [player]int
MyGame := class(creative_device):
# This is our scoreboard!
# It starts empty.
Scoreboard : PlayerScoreMap = {}
# This function runs when a player eliminates someone.
OnElimination := func(player: player):
# Check if the player is already on the scoreboard.
# If not, give them a score of 0.
if (Scoreboard[player] == 0):
Scoreboard[player] = 0
# Add 1 to their score.
# This updates the value for this player key.
Scoreboard[player] = Scoreboard[player] + 1
# Print the new score to the debug console.
print("Player scored! New total: ", Scoreboard[player])
# This runs when the game starts.
OnBegin<override>(sourced root: root):
print("Game Started! The scoreboard is ready.")
Walkthrough
PlayerScoreMap: We define a new type. It is a map. The key is aplayer. The value is anint.Scoreboard: We create a variable. It holds our map. It starts empty{}.Scoreboard[player]: This looks up the player. It finds their current score.Scoreboard[player] + 1: We take the old score. We add one. We save it back.
Try It Yourself
Now it is your turn.
Challenge: Make the scoreboard show the winner.
When a player reaches 5 eliminations, print "Winner!" to the console.
Hint: Use an if statement inside the OnElimination function. Check if the score is greater than or equal to 5.
Recap
You learned how to use a map in Verse. A map links keys to values. You used a player as the key. You used a number as the value. This helps you track scores in your game.
References
- 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/team-elimination-game-in-verse
- https://dev.epicgames.com/documentation/en-us/fortnite/team-elimiation-8-final-result-in-verse
- https://dev.epicgames.com/documentation/en-us/uefn/build-a-game-in-unreal-editor-for-fortnite
- https://dev.epicgames.com/documentation/fortnite/verse-elimination-template-in-unreal-editor-for-fortnite
Verse source files
- 01-device.verse · device
Turn this into a guided course
Add team-elimination-4-tracking-players-using-maps-in-verse 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.