Unlock the Treasure: Vending Machines with Verse
Unlock the Treasure: Vending Machines with Verse
Imagine a treasure chest that stays locked until you find the key. That is exactly what we are building today! We will make a Vending Machine that is broken at first. Then, we will use Verse code to fix it and give players a cool item.
What You'll Learn
- How to make a device stay "sleeping" at the start.
- How to write a simple Verse script to wake it up.
- How to connect a button press to an action.
How It Works
Think of a Vending Machine like a robot friend. When the game starts, this robot is asleep. It cannot sell anything. This is good! You do not want players to get infinite gold weapons right away.
We need a "wake-up call." In Verse, we call this enabling the device. It means turning it from "off" to "on."
To make this happen, we use two tools:
- The Device Settings: We tell the Vending Machine, "Do not work when the game starts."
- The Verse Script: We write a small rule. "When the player presses a button, wake up the Vending Machine."
This is like a secret door. The door looks closed. But when you push the right button, the mechanism clicks open.
Let's Build It
We will build a simple shop. First, the shop is closed. Then, you press a button, and the shop opens.
Step 1: Place the Devices
- Open your UEFN island.
- Place a Vending Machine device.
- Place a Button device near it.
- Put some items in the Vending Machine (like a shield potion).
Step 2: Configure the Vending Machine
Click on the Vending Machine to customize it. Change these settings:
- Enabled At Game Start: Set this to No.
- Why? This keeps the machine locked until we tell it to open.
- First Item Resource Type: Set this to Gold.
- Why? So players can buy things with gold.
Step 3: Write the Verse Code
Now, let's make the Button wake up the Machine. Create a new Verse script.
using { /Fortnite.com/Devices }
using { /Verse.org/Simulation }
using { /UnrealEngine.com/Temporary/Diagnostics }
# This is our main script block
MyVendingMachineScript := class(creative_device):
# 1. Link the devices here
# We need to connect the Button and the Vending Machine
@editable
my_button: button_device = button_device{}
@editable
my_shop: vending_machine_device = vending_machine_device{}
# 2. The "On Begin Play" event
# This runs once when the island starts
OnBegin<override>()<suspends>: void =
Print("Game starting! Shop is closed.")
# Subscribe to the button's interaction event
# InteractedWithEvent fires when a player presses the button
my_button.InteractedWithEvent.Subscribe(OnInteract)
# 3. The "On Interact" event
# This runs when a player presses the button
OnInteract(Agent: agent): void =
Print("Button pressed! Opening shop...")
# This line wakes up the vending machine
# It uses the Enable() function from Verse
my_shop.Enable()
# Optional: disable the button so it can only be pressed once
my_button.Disable()
Walkthrough of the Code
my_buttonandmy_shop: These are variables. Think of them as name tags. We give them the name of the devices we placed in the editor.OnBegin<override>()<suspends>: This is an event. It is a moment in time. The game calls this function when the level loads. We just print a message here to say "Hello."OnInteract(Agent: agent): This is another event. It happens when someone interacts with the device this script is attached to. In our case, the Button.my_shop.Enable(): This is the magic line. It calls the functionEnable. A function is like a verb. It tells the machine to do something. Here, it tells the shop to stop sleeping.
Try It Yourself
Can you make the shop close again?
Challenge: Add a second button called "Close Shop." When pressed, it should disable the vending machine.
Hint:
Look for a function that does the opposite of Enable. It might be called Disable. Try using my_shop.Disable() in a new event!
Recap
You just made a smart vending machine!
- We set the machine to start disabled.
- We wrote a Verse script to listen for a button press.
- We used the
Enable()function to unlock the shop.
Great job, coder! You are one step closer to building your dream island.
References
- https://dev.epicgames.com/documentation/en-us/fortnite-creative/using-vending-machine-devices-in-fortnite-creative
- https://dev.epicgames.com/documentation/en-us/fortnite/using-vending-machine-devices-in-fortnite-creative
- https://dev.epicgames.com/documentation/en-us/fortnite/5-rounds-of-econ-lessons-gameplay-example-in-fortnite-creative
- https://dev.epicgames.com/documentation/en-us/fortnite/verse-api/fortnitedotcom/devices/vending_machine_device/enable
- https://dev.epicgames.com/documentation/fortnite/verse-api/fortnitedotcom/devices/vending_machine_device/enable
Verse source files
- 01-device.verse · device
Turn this into a guided course
Add Enable or Disable Vending Machines 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.