The Magic Map: Finding Your Way in Verse
The Magic Map: Finding Your Way in Verse
Have you ever walked into a room and felt lost? Maybe you couldn't find the toy box. Or maybe you opened a fridge and it was empty! In programming, we call this a "Page Not Found" error. It just means the computer looked for something, but it wasn't there. Today, we will build a fun game to fix this problem. We will make a treasure hunt where the game checks if your treasure chest exists before you try to open it.
What You'll Learn
- What a "Page Not Found" error really means in games.
- How to check if an object exists in your world.
- How to use simple logic to keep your game from breaking.
How It Works
Imagine you are playing hide-and-seek. You want to find your friend. You look behind the big tree. If your friend is there, you say, "Found you!" If the tree is empty, you don't scream. You just say, "Not here," and move on.
In Verse, we do something similar. Sometimes, we want to do something with a prop, like a chest or a door. But what if that prop is missing? Maybe the player deleted it. Maybe it hasn't spawned yet. If we try to open a chest that isn't there, the game might crash! This is like trying to open a door that doesn't exist. It confuses the computer.
To fix this, we use a Check. A check is like a peek. We peek to see if the object is there. If it is, we open it. If it isn't, we stay safe. We don't crash. We just wait or do something else. This keeps our island running smoothly.
Let's Build It
We will build a simple treasure chest. When a player clicks it, the game checks if the chest is still there. If it is, it gives a coin. If it is gone, it says, "No treasure!"
Here is the code. Copy this into a Verse script in UEFN.
# This is our main script file.
# It lives on a device in your island.
using { /Fortnite.com/Devices }
using { /Fortnite.com/Characters }
using { /Verse.org/Simulation }
using { /UnrealEngine.com/Temporary/Diagnostics }
# We create a new type for our Treasure Device.
# This is like a blueprint for our special chest.
treasure_hunter := class(creative_device):
# This is a trigger_device. It is like a radio button.
# When the player walks into it, this device fires its event.
# Place a trigger_device on your island and connect it here.
@editable
TriggerDevice : trigger_device = trigger_device{}
# This is our main function. It runs when the game starts.
OnBegin<override>()<suspends> : void =
# We subscribe to the trigger's activated event.
# The game calls our handler whenever a player activates the trigger.
TriggerDevice.TriggeredEvent.Subscribe(OnTriggered)
# This function runs each time the trigger fires.
# Agent is the player (or object) that activated the trigger.
OnTriggered(Agent : ?agent) : void =
# Here is the magic check!
# We try to cast Agent to a fort_character to confirm a real player triggered it.
# note: Verse does not expose a bare IsAlive(); instead we check the cast succeeds.
if (ActualAgent := Agent?, Character := ActualAgent.GetFortCharacter[]):
# If yes, we are safe to do stuff.
Print("You found the treasure! 🎉")
# note: No generic GiveItem API exists in public Verse; we print a
# reward message as a stand-in for wiring an item-granter device.
Print("A coin has been added to your inventory!")
else:
# If we could not confirm a live character, we say so.
Print("The treasure is gone! 💨")```
### Walkthrough
1. **`treasure_hunter := class(creative_device)`**: This creates a new kind of device. Think of it like a custom LEGO brick. It has special powers.
2. **`TriggerDevice : trigger_device`**: This is the trigger. It waits for a player to activate the device on your island. Mark it `@editable` so you can plug in a real trigger from the UEFN editor.
3. **`if (Character := Agent.GetFortCharacter[])`**: This is the most important part. `Agent` is whoever set off the trigger. `GetFortCharacter[]` is a **failable** call — it only succeeds if the agent is a living player character in the world. It is like asking, "Is the box still on the shelf?"
4. **`Print(...)`**: This sends a message to the debug log. It is like the computer talking to you.
## Try It Yourself
Now it is your turn!
**Challenge:** Change the message when the treasure is gone. Instead of saying "The treasure is gone!", make it say "Oops! The treasure flew away!"
**Hint:** Look at the `else` part of your code. That is where the "not found" message lives. Change the words inside the quotes.
## Recap
We learned that "Page Not Found" is just a fancy way of saying "I couldn't find the thing I was looking for." In Verse, we use checks like `IsAlive` to peek before we act. This keeps our games safe and fun. Always check if your objects are there before you try to use them!
## References
- https://dev.epicgames.com/documentation/en-us/fortnite/verse-language-quick-reference
- https://dev.epicgames.com/documentation/en-us/fortnite-creative/using-timed-objective-devices-in-fortnite-creative
- https://dev.epicgames.com/documentation/fortnite/verse-api/versedotorg/verse/operator()-2
- https://dev.epicgames.com/documentation/fortnite/verse-api/versedotorg/verse/operator()
- https://dev.epicgames.com/documentation/fortnite/verse-api/versedotorg/verse/operator()-1
Verse source files
- 01-device.verse · device
Turn this into a guided course
Add Page not found 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.