Build a Magical Music Box with Verse
Unverified — this example may not compile as-is.
The Verse code below did not pass our automated compile check, so it isn't marked verified and is hidden from the guide listing. The explanation may still be useful, but treat the code as a draft and adapt it before use.
Build a Magical Music Box with Verse
Imagine a music box that plays a different tune every time you open it. Or a trap that only triggers when a specific musical note is hit. Today, we will build a "Musical Memory Box." It will remember a short melody and play it back when you press a button. You don’t need to know how to play an instrument. You just need to know how to give instructions to a computer!
What You'll Learn
- What a Note Sequencer is (it’s like a digital piano roll).
- How to use Verse to tell a device what notes to play.
- How to connect devices together using Events.
- How to make your island interactive with a simple button press.
How It Works
Think of a Note Sequencer like a slot machine for music. Instead of spinning wheels, it spins through a list of musical notes. One by one, it says, "Now play C!", then "Now play D!", then "Now play E!".
In Fortnite Creative, we use Patchwork Devices to make this happen. A Patchwork Device is like a smart gadget. It has inputs (where signals come in) and outputs (where signals go out).
We will use the Note Sequencer Device. This device holds a list of notes. When we tell it to start, it sends those notes to another device, like an Instrument Player. The Instrument Player is like a speaker that actually makes the sound.
But here is the cool part: we can use Verse to control the Note Sequencer. Verse is the code language that lets us say, "Start the music!" or "Change the tempo!" without clicking buttons manually.
The Scene Graph: Connecting the Dots
In Fortnite, every object is part of a Scene Graph. This is just a fancy name for the family tree of your island.
- The Island is the parent.
- The Devices are the children.
- The Verse Script is the brain that talks to the children.
When we write Verse, we are giving the brain instructions. We tell the brain to find the Note Sequencer and say, "Play your list!"
Let's Build It
We will build a system with three parts:
- A Button (Input).
- A Note Sequencer (The Memory).
- An Instrument Player (The Speaker).
Here is the Verse code to make the Note Sequencer play a simple 4-note tune when the button is pressed.
using { /Fortnite.com/Devices }
using { /Fortnite.com/Devices/Patchwork }
# This is our main script. It lives on a Verse Device.
# We give it a name so we can find it later.
MyMusicBox := class(VerseDevice):
# We need to tell Verse where our devices are.
# Think of these as "slots" for your gadgets.
ButtonInput := property(
Get():
return Button
)
# This is the Note Sequencer. It holds the notes.
Sequencer := property(
Get():
return NoteSeq
)
# This is the speaker that makes sound.
Speaker := property(
Get():
return InstrumentPlayer
)
# When the game starts, we set up our music.
OnBegin<override>()<suspends>:
# Let's make a simple scale: Do, Re, Mi, Fa.
# We use the Sequencer to set these notes.
Sequencer.SetNotes([
Note{Pitch: 60, Duration: 0.5},
Note{Pitch: 62, Duration: 0.5},
Note{Pitch: 64, Duration: 0.5},
Note{Pitch: 65, Duration: 0.5}
])
# Connect the Sequencer to the Speaker.
# This is like plugging a cable into a socket.
Sequencer.Outputs.NoteOutput.Connect(Speaker.Inputs.NoteInput)
# This function runs when the button is pressed.
OnButtonPressed():
# Tell the Sequencer to start playing!
Sequencer.Play()
Walkthrough: What Does This Code Do?
usingstatements: These lines tell Verse, "I want to use devices from Fortnite." It’s like saying, "I want to use my LEGO bricks."class(VerseDevice): This creates a new kind of device. It’s a container for our code.OnBegin: This function runs once when the game starts. We use it to load our notes. We create a list ofNoteobjects. Each note has a Pitch (how high or low it is) and a Duration (how long it lasts).Connect: This is the magic line. It connects the output of the Sequencer to the input of the Speaker. Now, when the Sequencer plays a note, the Speaker hears it.OnButtonPressed: This is where we need a little help from the editor. In the UEFN editor, you will attach this script to a device. You will also drag your Button into theButtonInputslot. When the button is clicked, this code runs, andSequencer.Play()is called.
Building It in UEFN
- Place a Note Sequencer device on your island. Name it
NoteSeq. - Place an Instrument Player device. Name it
InstrumentPlayer. - Place a Button device. Name it
Button. - Create a new Verse Device and paste the code above.
- In the Details Panel of your Verse Device, you will see slots for
ButtonInput,Sequencer, andSpeaker. - Use the eyedropper tool to drag your
Button,NoteSeq, andInstrumentPlayerinto those slots. - Click Launch Session. Press the button. Listen to your music!
Try It Yourself
You made a 4-note tune! Now, let’s make it more interesting.
Challenge: Change the melody.
Hint: Look at the OnBegin function. You will see a list of notes inside square brackets [ ]. Try changing the Pitch numbers.
- Lower numbers are deeper sounds.
- Higher numbers are higher sounds.
- Try adding more notes to the list!
Don’t worry if it sounds silly at first. That’s how composers practice!
Recap
Today you learned how to use the Note Sequencer API in Verse. You learned that a Note Sequencer holds a list of notes, and you can connect it to an Instrument Player to make sound. You also learned how to use OnBegin to set up your music and how to connect devices together. Great job making music with code!
References
- https://dev.epicgames.com/documentation/en-us/fortnite-creative/using-patchwork-note-sequencer-devices-in-fortnite-creative
- https://dev.epicgames.com/documentation/en-us/fortnite/using-patchwork-note-sequencer-devices-in-fortnite-creative
- https://dev.epicgames.com/documentation/en-us/fortnite/verse-api/fortnitedotcom/devices/patchwork/note_sequencer_device
- https://dev.epicgames.com/documentation/fortnite/verse-api/fortnitedotcom/devices/patchwork/note_sequencer_device
- https://dev.epicgames.com/documentation/en-us/fortnite-creative/using-patchwork-note-trigger-devices-in-fortnite-creative
Verse source files
- 01-standalone.verse · standalone
Turn this into a guided course
Add Note Sequencer API 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.