The Loot Goblin’s Ledger: Mastering Math in Verse
Tutorial beginner compiles

The Loot Goblin’s Ledger: Mastering Math in Verse

Updated beginner Code verified

The Loot Goblin’s Ledger: Mastering Math in Verse

Welcome to the chaotic heart of UEFN. You’ve got triggers, you’ve got prop-movers, but right now your island is just a collection of static objects waiting for something to happen. To make a real game—where loot counts matter, damage scales, and shops actually work—you need to talk to your code using numbers.

In this tutorial, we’re going to build a Dynamic Loot Shop. No more static chests. We’re going to use Verse’s arithmetic (the math) to let players buy items with coins, subtract the cost from their wallet, and add the loot to their inventory. It’s the digital equivalent of trading your last stack of ammo for a healing potion, but with fewer awkward eye contacts.

What You'll Learn

  • Variables as Inventory Slots: How to store changing numbers (like coin counts) in Verse.
  • Operators as Actions: Using symbols like +, -, and * to perform math, just like calculating damage or building costs.
  • Compound Assignment: The shortcut way to update a number (like += and -=) without rewriting the whole line.
  • Floats vs. Integers: Understanding the difference between whole numbers (coins) and decimals (health or precision movement).

How It Works

Before we write a single line of Verse, let’s look at how math works in Fortnite. You already know this stuff; you just didn’t know it had a name.

1. Variables: The "Current Health" Bar

In Fortnite, your health bar isn’t a static number. It changes every time you take damage or use a medkit. In programming, a variable is just a named container that holds a value which can change. Think of it like your Current Health stat. You start with 100, but it’s a variable because it varies based on what happens in the game.

2. Operators: The "Damage" Button

An operator is a symbol that tells the computer to do something to data. In Verse, these are mostly math symbols.

  • + (Addition): Like picking up a shield potion (+25 shield).
  • - (Subtraction): Like taking damage (-15 health).
  • * (Multiplication): Like buying a stack of 50 ammo x 2 stacks.
  • / (Division): Like splitting a team into two equal squads.

3. Operands: The "Items" Being Modified

The numbers or variables you apply an operator to are called operands. If you write Health - 10, Health is the operand (the thing being changed) and 10 is the operand (the amount of change).

4. Floats vs. Integers: Coins vs. Precision

This is where beginners trip up.

  • Integer (int): Whole numbers. No decimals. This is perfect for Coins, Eliminations, or Ammo Count. You can’t have 3.5 coins.
  • Float (float): Numbers with decimals. This is perfect for Health (75.5 HP), Movement Speed, or Storm Damage per tick.

Pro Tip: When doing math with coins, stick to int. When doing math with health or physics, use float.

Let's Build It

We are going to create a simple script that runs when a player touches a "Shop Zone." The script will check if the player has enough coins, subtract the cost, and grant them a weapon.

Here is the Verse code. Don’t worry if it looks alien; we’ll break it down line-by-line.

# Import the basic tools we need
using { /Fortnite.com/Devices }
using { /Verse.org/Simulation }
using { /Verse.org/Native }

# Define our Shop Logic
ShopLogic := class(creative_device):
    # 1. Define our Variables (The "Inventory Slots")
    # These are 'int' because coins are whole numbers
    var PlayerCoins : int = 100          # Starting gold in the shop
    var ItemCost : int = 25              # Cost of the weapon
    var ItemName : string = "Assault Rifle" # What we are selling

    # 2. The Function (The "Trigger" Action)
    # This runs when we call BuyItem()
    BuyItem() : void =
        # Check if we have enough coins (Equality Operator: =)
        if (PlayerCoins >= ItemCost):
            # 3. Compound Assignment (The Shortcut)
            # Subtract cost from coins: PlayerCoins = PlayerCoins - ItemCost
            set PlayerCoins -= ItemCost 
            
            # Add the item to the player (Simplified logic for demo)
            # In real UEFN, you'd use ItemGranter here
            Print("Purchased " + ItemName + "! Remaining Coins: " + string("{PlayerCoins}"))
        else:
            # Not enough gold!
            Print("Not enough gold! You need " + string("{ItemCost - PlayerCoins}") + " more.")

    # 4. The Start Event (When the game begins)
    OnBegin<override>()<suspends> : void =
        # Let's simulate a purchase
        BuyItem()
        # Let's simulate another purchase
        BuyItem()```

### Walkthrough: What Just Happened?

1.  **`var PlayerCoins : int = 100`**: We created a variable named `PlayerCoins`. Its an `int` (integer), so it can only hold whole numbers. We set it to 100. Think of this as the player walking into the shop with $100 in their pocket.
2.  **`if (PlayerCoins >= ItemCost)`**: This is a **Conditional Statement**. It asks a question: "Is the number in `PlayerCoins` greater than or equal to `ItemCost`?"
    *   If **Yes**: We enter the block inside the `if`.
    *   If **No**: We skip to the `else` block.
3.  **`set PlayerCoins -= ItemCost`**: This is the magic shortcut.
    *   `-= ` is a **Compound Assignment Operator**.
    *   It means: "Take the current value of `PlayerCoins`, subtract `ItemCost` from it, and save the result back into `PlayerCoins`."
    *   Its like saying: "My new balance is my old balance minus the price."
4.  **`Print(...)`**: This sends a message to the debug log (and in-game chat if configured). It uses the `+` operator to glue text strings together with our variable values.

### Try This in Your Island
1.  Place a **Trigger Volume** in your island.
2.  Add a **Verse Actor** component to it.
3.  Paste this code into the Verse Actor.
4.  Run your island. Watch the debug log. Youll see the coin count drop from 100 to 75, then to 50.

## Try It Yourself

Now that you know how to subtract, lets make the shop smarter.

**The Challenge:**
Modify the `BuyItem` function so that if the player tries to buy an item but *doesn't* have enough coins, the game doesn't just say "No." Instead, it calculates **how many more coins** they need and prints that specific number.

**Hint:**
You already know how to subtract (`-`). If `PlayerCoins` is 10 and `ItemCost` is 25, what math operation gives you the difference (15)? Use that inside your `else` block's `Print` statement.

*Don't peek at the solution if you want the satisfaction of solving it! But if you're stuck, remember: `Needed = Cost - Have`.*

## Recap

*   **Variables** are containers for changing data (like your coin count).
*   **Operators** (`+`, `-`, `*`, `/`) are the actions you perform on that data.
*   **Compound Assignment** (`-=`, `+=`) is a shorthand way to update a variables value based on its current value.
*   **Integers** are for whole numbers (coins, kills), while **Floats** are for decimals (health, speed).

You now have the mathematical tools to build shops, damage systems, and score trackers. The storm isnt going to wait for you, so go make something profitable.

## References
- https://dev.epicgames.com/documentation/en-us/uefn/learn-code-basics-2-basic-programming-components-in-verse
- https://dev.epicgames.com/documentation/en-us/uefn/float-in-verse
- https://dev.epicgames.com/documentation/en-us/fortnite/float-in-verse
- https://dev.epicgames.com/documentation/en-us/fortnite/operators-in-verse
- https://dev.epicgames.com/documentation/en-us/uefn/int-in-verse

Verse source files

Turn this into a guided course

Add data-manipulation-and-arithmetic 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