The Infinite Snack Machine
Tutorial beginner compiles

The Infinite Snack Machine

Updated beginner Code verified

The Infinite Snack Machine

Imagine you are playing a game. You find a magic vending machine. Every time you eat a snack, a new one appears instantly. You never run out. This is super cool. It keeps the game fun. You can keep playing forever. We will build this machine today. We will use Verse to make it happen. You will learn how to code a loop. A loop is like a circle. It repeats actions over and over. Let's start coding!

What You'll Learn

  • How to use a Switch device.
  • How to use an Item Spawner device.
  • How to connect devices with wires.
  • How to make items appear forever.

How It Works

Think of a loop like a record player. The needle goes around and around. It plays the same song again and again. In our game, we want items to appear again and again. We need two main parts.

First, we need a Switch. A switch is like a light switch in your room. It turns things on or off. We will use it to start the machine.

Second, we need an Item Spawner. This device holds items. It drops them on the floor. Think of it like a cookie jar. You open the jar. Cookies fall out.

Here is the magic trick. We connect the Switch to the Spawner. When the Switch turns on, it sends a signal. The signal tells the Spawner to drop an item. Then, the Spawner sends a signal back to the Switch. This creates a circle. The circle never stops. Items keep falling. You have an infinite snack machine!

Let's Build It

We will write a small Verse script. This script controls the devices. It makes the loop work. Follow the steps below.

  1. Place an Item Spawner in your island.
  2. Place a Switch near it.
  3. Drop some items (like a shield potion) into the Spawner.
  4. Open the Verse editor for the Spawner.
  5. Paste the code below.
using { /Fortnite.com/Devices }
using { /Verse.org/Simulation }

# This is our main script block.
# It connects our devices.
snack_machine := class(creative_device):

    # These properties are set in the UEFN editor.
    # Drag your Item Spawner device into MySpawner.
    # Drag your Switch device into MySwitch.
    @editable
    MySpawner : item_spawner_device = item_spawner_device{}

    @editable
    MySwitch : switch_device = switch_device{}

    # This is the main function.
    # It runs when the game starts.
    OnBegin<override>()<suspends> : void =
        # Wait for the switch to be turned on.
        # This is like waiting for the player to flip the switch.
        MySwitch.TurnedOnEvent.Await()

        # Start an infinite loop.
        # This keeps running forever.
        loop:
            # Wait for a tiny moment.
            # This gives the game time to catch up.
            # Without this, the game might freeze.
            Sleep(0.1)

            # Tell the spawner to drop an item.
            MySpawner.Enable()

            # The loop goes back to the top.
            # It waits again, then drops another item.```

### Walkthrough

Let's look at the code. It has three parts.

First, we define our devices. We name them `MySpawner` and `MySwitch`. This is like giving names to your toys. Now we know which is which.

Second, we have `OnBegin`. This runs when the game starts. It waits for the switch. It does not do anything yet. It is waiting for the player.

Third, we have the `loop` block. This is the heart of the machine. It waits a little bit. Then it drops an item. Then it waits again. This happens forever. The player can pick up items. New ones appear right away.

## Try It Yourself

You built the infinite machine! Now, let's make it harder. Can you make the items appear slower? Try changing the wait time.

**Challenge:** Change `0.1` to `1.0`. What happens?

**Hint:** A higher number means a longer wait. The items will appear less often. Try `0.5` for a medium speed. Experiment with different numbers. See what feels best for your game.

## Recap

You made an infinite snack machine! You used a Switch to start the game. You used an Item Spawner to drop items. You used a loop to repeat the action. Loops are powerful tools. They let you repeat tasks easily. You can use this for ammo, health, or traps. Keep building and have fun!

## References

*   https://dev.epicgames.com/documentation/en-us/fortnite/using-item-spawner-devices-in-fortnite-creative
*   https://dev.epicgames.com/documentation/en-us/fortnite-creative/using-item-spawner-devices-in-fortnite-creative
*   https://dev.epicgames.com/documentation/en-us/fortnite/create-a-free-for-all-game-in-fortnite-creative
*   https://dev.epicgames.com/documentation/en-us/uefn/escape-room-9-inside-cabin-in-unreal-editor-for-fortnite
*   https://dev.epicgames.com/documentation/en-us/fortnite-creative/using-portable-consumables-in-fortnite-creative

Verse source files

Turn this into a guided course

Add Continuous Item Spawning 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