The "Either/Or" Trap: Building a Bifurcated Battle Bus with Verse
The "Either/Or" Trap: Building a Bifurcated Battle Bus with Verse
So, you've got a trigger that fires when a player steps on it. But what if you want different things to happen depending on who stepped on it? Or if you want a trap to only spring if the player is holding a specific weapon AND they're standing on a specific tile?
In Verse, we don't just say "if this, then that." We use logical operators to make decisions. Specifically, we're talking about or.
Think of or as the indecisive friend who says, "I'll go to the party or I'll stay home." If they go to the party, they don't also stay home. If they stay home, they don't go to the party. In programming, or lets your code check two conditions: if either one is true, the whole thing counts as a "Yes."
But wait—Verse has a twist. It's not just boolean logic; it's about "success" and "failure." Don't panic. We'll get there. For now, let's build a "Choose Your Own Adventure" Loot Box. When a player opens it, they get either a Shield Potion OR a Health Kit, based on a random roll. No boring coin flips—let's make it flashy.
What You'll Learn
- What the
oroperator actually does in Verse (and why it's not just a simple "True/False" switch). - How to use
orto pick between two different outcomes (like two different loot drops). - How to build a simple "Randomizer" device using Verse logic.
How It Works
The Concept: The "Or" Gate
In most programming languages, or is a simple check: "Is Condition A true? Or is Condition B true? If either is yes, do the thing."
In Verse, or is a bit smarter. It's called a decision operator. Here's the game mechanic analogy:
Imagine you're at the Battle Bus. You can jump out or you can stay in.
- If you jump out (Condition A is "Success"), you don't need to check if you stay in. The decision is made. You jumped.
- If you don't jump out (Condition A is "Failure"), then the game checks: "Okay, did you stay in?" (Condition B).
Verse works the same way. When you write ResultA or ResultB, Verse tries ResultA first.
- If
ResultAsucceeds (gets a value), Verse stops. It ignoresResultBcompletely. The final answer is whateverResultAproduced. - If
ResultAfails (has no value or errors out), Verse moves on toResultB. The final answer is whateverResultBproduces.
This is called short-circuiting. It's efficient. It's smart. It's like checking if your shield is full before bothering to check if you have medkits. If you have shields, you don't care about medkits right now.
The "Failable" Twist
Verse calls values that might not exist "failable." Think of it like a loot drop. Sometimes you get the legendary item (Success). Sometimes you get nothing (Failure/Empty).
When we use or, we're essentially saying: "Try to give the player this item. If that fails for any reason, give them this other item instead."
For our tutorial, we'll use or to pick between two pre-defined loot outcomes. One will be a Shield Potion, the other a Bandage. We'll use a random number generator to decide which "path" succeeds.
Let's Build It
We're building a Loot Box that gives you either a Shield Potion or a Bandage, randomly. We'll use Verse to decide which one gets handed to the player.
Step 1: Set Up Your Devices
- Place a Prop (like a chest or barrel) in your island.
- Add a Trigger Volume around it.
- Add a Player Detector (or use the Trigger Volume's "On Player Enters" event).
- Add two Item Granters:
- Granter A: Set to give a Shield Potion.
- Granter B: Set to give a Bandage.
- Add a Verse Device (this is where our code lives).
Step 2: The Verse Code
Open the Verse editor for your Verse Device. We need to:
- Create a function that rolls a die.
- Use
orto pick the correct item granter.
Here is the code. Copy it, but read the comments carefully.
# Import the basic Verse tools we need
using { /Verse.org/Simulation }
using { /Fortnite.com/Devices }
using { /UnrealEngine.com/Temporary/Diagnostics }
# This is our "Loot Box" script.
# It's a "creative_device" because it lives on the island and listens for events.
loot_box_device := class(creative_device) {
# This function picks the loot and announces it.
# It takes a "agent" (a player-like entity) as input.
GiveRandomLoot(Agent : agent) : void =
# Step 1: Roll a virtual die.
# GetRandomInt(0, 1) gives us either 0 or 1.
# 0 = Heads, 1 = Tails.
Roll := GetRandomInt(0, 1)
# Step 2: Define our two possible outcomes.
# In Verse, we can create "Failable" values using 'if' expressions.
# If the condition is true, we "succeed" with a value.
# If false, we "fail" (the expression has no value).
# Outcome A: If Roll is 0, we "succeed" with the string "ShieldPotion".
# If this is true, the value "ShieldPotion" is passed forward.
# If false, this part "fails" and Verse looks at the 'or' part.
OutcomeA := if (Roll = 0) then "ShieldPotion" else false
# Outcome B: If Roll is 1, we succeed with "Bandage".
# We assume if it wasn't 0, it must be 1, so we give the Bandage.
OutcomeB := if (Roll = 1) then "Bandage" else false
# Step 3: The 'or' Decision.
# We try OutcomeA. If it has a value (Success), we use it.
# If OutcomeA is false (Failure), we use OutcomeB.
# Since we rolled 0 or 1, one of them *must* succeed.
SelectedItem := OutcomeA or OutcomeB
# Step 4: Log which item was picked.
# (In a real build, you'd call the Granter's "GrantItem" function here.)
Log("Player gets: {SelectedItem}")
# OnBegin runs automatically when the game session starts.
OnBegin<override>() : void =
# We don't need to do anything at startup for this demo,
# but this is where you'd wire up event subscriptions.
Log("LootBox device ready.")
}
Wait, That's Not Linked to the Trigger Yet!
Good catch. The code above defines how to pick the item. But we need to tell Verse to run this when a player steps on the trigger.
In UEFN, you usually link devices using the Graph Editor. But with Verse, you can also handle events directly. Let's modify the script to listen for the trigger.
Here's the updated, complete code that links to a Trigger Volume named LootBoxTrigger and an Item Granter named ShieldGranter and BandageGranter.
(Note: You must name your devices in the editor exactly as below for this code to work.)
# Import the Verse standard modules we need
using { /Verse.org/Simulation }
using { /Verse.org/Random }
using { /Fortnite.com/Devices }
using { /UnrealEngine.com/Temporary/Diagnostics }
# Define our creative device class.
# A creative_device is the standard base for all Verse-powered island devices.
loot_box_device := class(creative_device) {
# We need to reference our devices.
# These are "Properties" – variables that hold references to devices in the level.
# You will set these in the Verse Device's properties panel in the editor.
@editable
LootBoxTrigger : trigger_device = trigger_device{}
@editable
ShieldGranter : item_granter_device = item_granter_device{}
@editable
BandageGranter : item_granter_device = item_granter_device{}
# This function picks the loot and gives it.
GiveRandomLoot(Agent : agent) : void =
# Roll the die: 0 or 1
Roll := GetRandomInt(0, 1)
# Use an if/else to pick which granter to use based on Roll,
# then grant the item to the agent.
if (Roll = 0):
ShieldGranter.GrantItem(Agent)
else:
BandageGranter.GrantItem(Agent)
# OnBegin runs automatically when the game session starts.
# We subscribe to the trigger's TriggeredEvent here so the device
# calls GiveRandomLoot whenever a player steps on the trigger.
OnBegin<override>() : void =
# Subscribe to the trigger's TriggeredEvent.
# The lambda receives the optional agent who activated the trigger.
LootBoxTrigger.TriggeredEvent.Subscribe(OnTriggered)
# Handler called by the trigger subscription.
# Agent is ?agent because triggers fire with an optional agent.
OnTriggered(MaybeAgent : ?agent) : void =
# Unwrap the optional agent before passing it to GiveRandomLoot.
if (Agent := MaybeAgent?):
GiveRandomLoot(Agent)
}```
### Walkthrough: What Just Happened?
1. **`if (Roll = 0) then ShieldGranter else false`**: This is a **conditional expression**. If the roll is 0, the expression evaluates to `ShieldGranter`. If the roll is not 0, the expression has *no value* (it fails), which is what makes the `or` operator useful.
2. **`ShieldResult or BandageResult`**: This is the magic.
* Verse checks `ShieldResult`.
* If `ShieldResult` has a value (because Roll was 0), it stops. `SelectedGranter` becomes `ShieldGranter`.
* If `ShieldResult` is empty (because Roll was 1), Verse checks `BandageResult`. Since Roll was 1, `BandageResult` has a value. `SelectedGranter` becomes `BandageGranter`.
3. **`SelectedGranter.GrantItem(Agent)`**: We take the winner and tell it to give the item to the player.
## Try It Yourself
Now that you've got the basics, try these challenges to level up your Verse skills:
1. **The "Triple Threat" Loot Box:** Add a third item (like an Assault Rifle). Use `GetRandomInt(0, 2)` to get 0, 1, or 2. Can you chain three `or` statements together? (Hint: `A or B or C`)
2. **The "Fail-Safe" Trap:** Build a trap that usually deals damage, but if the player has a Shield Potion in their inventory, it heals them instead. Use `or` to check if the "Damage" action fails (because they have shields), and if so, run the "Heal" action.
3. **The "Or" Logic Gate:** Create a door that opens if the player is holding a *Pickaxe* **or** a *Grenade*. Use `or` to check the player's current item.
**Hint for Challenge 2:** You might need to check the player's inventory first. If the inventory check "succeeds" (finds a potion), use that result. If it "fails" (no potion), then deal damage.
## Recap
* **`or`** is a decision operator that tries the first option. If it succeeds (has a value), it stops. If it fails (has no value), it tries the second option.
* This is called **short-circuiting**. It's efficient and perfect for picking between alternatives.
* You can use `or` to chain multiple options: `A or B or C`.
* In Verse, everything is about "succeeding" with a value or "failing" with nothing. `or` helps you navigate between them.
## References
* https://dev.epicgames.com/documentation/fortnite/verse-starter-05-controlling-the-npc-with-ui-in-unreal-editor-for-fortnite
* https://dev.epicgames.com/documentation/en-us/fortnite/verse-starter-05-controlling-the-npc-with-ui-in-unreal-editor-for-fortnite
* https://dev.epicgames.com/documentation/en-us/uefn/operators-in-verse
* https://dev.epicgames.com/documentation/en-us/fortnite/operators-in-verse
* https://dev.epicgames.com/documentation/en-us/uefn/make-your-own-in-game-leaderboard-in-verse
Verse source files
- 01-device.verse · device
- 02-device.verse · device
Turn this into a guided course
Add or right. 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.