The Magic Box: Storing Information in Verse
The Magic Box: Storing Information in Verse
Welcome, young coders! Today, we are going to learn how to make your Fortnite island remember things.
Imagine you are playing a game. You pick up a shield. Now you have 50 shields. Later, you pick up another one. Now you have 100! How does the game know you have 100? It uses a variable.
A variable is like a magic box. You can put things in it. You can take things out. And you can change what is inside.
In this lesson, we will build a simple "Health Station." When players touch it, their health goes up. We will use Verse to store that health number.
What You'll Learn
- What a variable is and why we need it.
- How to create a variable in Verse.
- How to change a variable when a player does something.
- How to use a constant for things that never change.
How It Works
Let’s think about a video game like a toy chest.
Inside the chest, there are different compartments. One compartment holds your score. Another holds your ammo count. Another holds your character’s health.
In programming, we call these compartments variables.
What is a Variable?
A variable is a name for a piece of data. Data is just information. Numbers, words, or colors are all data.
Think of a variable like a labeled jar.
- You give the jar a name, like "Health."
- You put a number inside, like
100. - If you get hurt, you take out
100, subtract10, and put90back in.
The value inside the jar changes. This is why we call it mutable. Mutable means it can change.
What is a Constant?
Sometimes, we have things that should never change.
- The number of seconds in a minute is always
60. - The color of the sky in a clear day is always blue (usually!).
In Verse, we call these constants. A constant is a box you seal shut. You can read what is inside, but you cannot change it. This helps keep your code safe and correct.
The Scene Graph Connection
In Fortnite islands, everything is part of a Scene Graph. Think of the Scene Graph as a giant family tree for your island.
- The root is the whole island.
- Branches are groups of objects.
- Leaves are individual items like a chair or a player.
When we write code, we often attach variables to specific parts of this tree. For example, we might attach a "Health" variable to a specific health station prop.
Let's Build It
We will build a simple Health Station. When a player touches it, they gain health. We will store the current health in a variable.
Step 1: Create the Health Station
- Open UEFN.
- Place a Prop Mover or a simple Static Prop in your island. Let’s call it "HealthStation."
- Add a Trigger Volume around it. This detects when a player walks in.
- Add a Verse Device to the Trigger Volume.
Step 2: Write the Code
Open the Verse file for your Trigger Volume. We will write code that:
- Defines a variable for health.
- Changes that variable when the player enters.
Here is the code. Read it carefully.
# This is a comment. It helps us remember what the code does.
# We define a function that runs when the trigger is entered.
OnTriggerEnter := func(other_actor: Actor):
# 'other_actor' is the player who walked in.
# Let's find the player's health.
# Note: In real UEFN, you might use a specific Health Component.
# For this example, we will simulate a health variable.
# Create a variable called 'current_health'.
# It starts at 100. This is a mutable value.
current_health := 100
# Let's say the player gains 25 health.
# We add 25 to the current health.
current_health := current_health + 25
# Now, 'current_health' holds 125.
# In a real game, you would update the player's actual health here.
# For now, we just print it to the screen to see it work.
print("Player health is now: ", current_health)
Breaking Down the Code
current_health := 100: This creates a new variable namedcurrent_health. We give it the value100. The:=symbol means "create and assign."current_health := current_health + 25: This line updates the variable. It takes the old value (100), adds25, and saves the new result (125) back intocurrent_health.print(...): This shows text on the screen. It’s like a sticky note for the player to see what is happening.
Try It Yourself
Now it’s your turn!
Challenge:
Modify the code above. Instead of adding 25 health, make it add 50 health. Then, change the starting health to 50 instead of 100.
Hint:
Look for the line that says current_health := 100. Change the 100 to 50. Then look for the line that adds 25. Change the 25 to 50.
Run your island and test it!
Recap
- A variable is a named box that stores information. It can change.
- A constant is a named box that stores information. It cannot change.
- We use variables to track things like health, score, or ammo.
- In Verse, we use
:=to create and assign variables.
Great job! You now know how to store information in your code. Next, we will learn how to use that information to make cool things happen.
References
- https://dev.epicgames.com/documentation/en-us/fortnite/basics-of-writing-code-3-storing-and-using-information-in-verse
- https://dev.epicgames.com/documentation/en-us/fortnite/learn-the-basics-of-writing-code-in-verse
- https://dev.epicgames.com/documentation/en-us/uefn/learn-the-basics-of-writing-code-in-verse
- https://dev.epicgames.com/documentation/en-us/fortnite/basics-of-writing-code-1-basic-programming-terms-in-verse
- https://dev.epicgames.com/documentation/en-us/uefn/basics-of-writing-code-4-writing-simple-code-in-verse
Verse source files
- 01-fragment.verse · fragment
Turn this into a guided course
Add basics-of-writing-code-3-storing-and-using-information-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.