The Mystery Cooler: A Treasure Hunt with Verse
Tutorial beginner

The Mystery Cooler: A Treasure Hunt with Verse

Updated beginner

The Mystery Cooler: A Treasure Hunt with Verse

Do you like surprises? Do you like finding hidden treasures? Let’s build a mystery cooler!

This cooler will hide random snacks. Players will have to search the island to find it. When they open it, they get a surprise item. We will use Verse to make the cooler feel magical.

What You'll Learn

  • How to use a device in UEFN.
  • What a variable is (a box that holds a changing number).
  • How to make a random choice in code.
  • How to connect devices using events.

How It Works

Imagine you have a lunchbox. You put different snacks inside. You don’t know which one you will get. That is random!

In Fortnite Creative, we have a Cooler device. It is like a magic fridge. It holds food items. We can make it give out apples, peppers, or other treats.

We will use a variable. Think of a variable as a little jar. We put a number in the jar. This number tells the game which snack to pick.

We will also use an event. An event is like a doorbell. When a player touches the cooler, it rings. Our code hears the ring. Then, it picks a random snack.

This is how we make a game feel alive. The player never knows what they will get!

Let's Build It

First, place a Cooler device in your world. Name it MysteryCooler.

Next, we need some code. We will write a script that runs when someone opens the cooler.

Here is the code for your Verse script. Copy it into a new Verse file in your project.

# This is a Verse script for the Mystery Cooler

# We create a device for our cooler.
# Think of this as giving the cooler a name tag.
device: object = world.GetRootDevice()

# This is a function.
# A function is a list of steps to do.
# We call it "PickSnack".
PickSnack(): void =>
    # We make a random number between 1 and 3.
    # This is like rolling a 3-sided die.
    snack_choice := world.GetRandomInt(1, 3)
    
    # We check which number we rolled.
    if snack_choice == 1:
        # If it is 1, give an Apple.
        device.GrantItem("Apple")
    elif snack_choice == 2:
        # If it is 2, give a Pepper.
        device.GrantItem("Pepper")
    else:
        # If it is 3, give a Mushroom.
        device.GrantItem("Mushroom")

# This part connects the code to the game.
# It waits for the "On Interacted With" event.
# When this event happens, we run PickSnack().
device.OnInteractedWith(PickSnack)

What Does This Code Do?

  1. device: object: This line names our cooler. It lets Verse talk to the specific cooler you placed in the world.
  2. PickSnack(): This is our recipe. It has three steps inside.
  3. world.GetRandomInt(1, 3): This picks a random number. It is like flipping a coin, but with three options.
  4. if snack_choice == 1: This checks the number. If it is one, we give an Apple.
  5. device.OnInteractedWith: This is the trigger. It says, "When a player touches this cooler, run the PickSnack recipe."

Now, go to the Verse Editor in UEFN. Paste this code. Then, attach the script to your MysteryCooler device.

When you play your island, walk up to the cooler. Open it. You might get an apple! Or a pepper! It is a surprise every time.

Try It Yourself

Can you make the cooler give four different items?

Hint:

  1. Change the random number range from (1, 3) to (1, 4).
  2. Add a new elif line for the number 4.
  3. Give a fourth item, like a Banana or a Fish.

Remember to keep your code tidy. Use spaces to line up your if and elif statements. It helps you read it better!

Recap

You built a mystery cooler! You learned that:

  • A variable holds a changing number.
  • A function is a set of instructions.
  • An event starts the code when something happens.
  • Randomness makes games fun and surprising.

Great job! Your island is now more exciting.

References

  • https://dev.epicgames.com/documentation/en-us/fortnite-creative/using-chest-and-ammo-gallery-devices-in-fortnite-creative
  • https://dev.epicgames.com/documentation/en-us/fortnite/using-chest-and-ammo-gallery-devices-in-fortnite-creative
  • https://dev.epicgames.com/documentation/en-us/fortnite/fortnite-glossary
  • https://dev.epicgames.com/documentation/en-us/fortnite/hud-message-device-design-example-in-fortnite-creative
  • https://dev.epicgames.com/documentation/en-us/fortnite-creative/hud-message-device-design-example-in-fortnite-creative

Verse source files

Turn this into a guided course

Add Cooler 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