Make Music Change with the Value Setter
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.
Make Music Change with the Value Setter
Imagine you are playing a game. You are sneaking past guards. The music is quiet and spooky. Suddenly, you get caught! The music suddenly gets loud and fast. How does the game know to change the music? It uses a special device called the Value Setter.
In this tutorial, we will build a simple music switch. We will use Verse to connect a button to the music. When you press the button, the music will change. It is like a light switch for sound!
What You'll Learn
- What a Value Setter is.
- How to use Verse to send signals to devices.
- How to make music change during gameplay.
- The basics of connecting devices together.
How It Works
Think of your Fortnite island like a house. The Patchwork device is the stereo system. It plays the music. The Value Setter is a remote control. It tells the stereo what to do.
In Fortnite Creative, we have devices. Devices are like tools. They do specific jobs.
- The Music Player plays the song.
- The Value Setter changes the settings of the music player.
We use Verse to write the rules. Verse is the language of logic. It tells the devices when to talk to each other.
Here is the plan:
- We place a Button in the game.
- We place a Music Player in the game.
- We write Verse code. The code says: "When the button is pressed, tell the music player to change the volume."
The Value Setter is the bridge. It takes the signal from the button. It then sets a value on the music player. A value can be anything. It can be volume. It can be speed. It can be volume zero to turn it off!
Let's Build It
We will write a small script. This script will make the music fade out when you press a button.
First, open UEFN. Create a new island. Add a Button device. Add a Music Player device. Name them clearly. Let's call the button MyButton and the music MyMusic.
Now, let's look at the Verse code. This code connects them.
# This is the main script for our music switch.
using { /Fortnite.com/Devices }
using { /Fortnite.com/Devices/Patchwork }
using { /Verse.org/Simulation }
using { /UnrealEngine.com/Temporary/Diagnostics }
MyMusicSwitch := class(creative_device):
# This is the button device.
@editable
ButtonDevice : button_device = button_device{}
# This is the music device.
@editable
MusicDevice : speaker_device = speaker_device{}
# This is the Value Setter device.
# It connects the button to the music.
@editable
VolumeSetter : value_setter_device = value_setter_device{}
# This function runs when the game starts.
OnBegin<override>()<suspends> : void =
# We connect the button's "Pressed" event to our script.
ButtonDevice.InteractedWithEvent.Subscribe(OnButtonPressed)
# This function runs when the button is pressed.
OnButtonPressed(Agent : agent) : void =
# We use the Value Setter to change the volume.
# We set it to 0.0 to turn the music off.
VolumeSetter.SetValue(0.0)```
Let's walk through the code.
The first part is the **class**. A class is like a blueprint. It holds all the devices we need. We use `@editable` to mark them. This means we can pick the devices in the editor.
Next is the `OnBegin` function. This runs once. It sets up the connection. It says, "Watch the button. If it gets pressed, run `OnButtonPressed`."
Finally, `OnButtonPressed` does the work. It calls `SetValue` on the `VolumeSetter`. The `VolumeSetter` then changes the music player's volume to zero. The music stops!
You can also set the volume to `1.0` to make it loud again. Try changing the number in the code.
## Try It Yourself
Now it is your turn to build!
**Challenge:** Make the music start quiet and get louder when you press the button.
**Hint:**
1. Set the initial volume of the music player to `0.2` in the editor.
2. In the `OnButtonPressed` function, change the value to `1.0`.
3. Test your island. Press the button. Does the music get louder?
If it works, you have mastered the Value Setter!
## Recap
The Value Setter is a powerful tool. It lets you control game devices with code. You can change volume, speed, and more. By using Verse, you can make your island interactive. You are no longer just placing objects. You are creating experiences. Keep experimenting. Have fun making your island unique!
## References
* https://dev.epicgames.com/documentation/en-us/fortnite-creative/build-patchwork-x-yacht-heist-1-adaptive-soundtrack-in-fortnite-creative
* https://dev.epicgames.com/documentation/en-us/fortnite/yacht-heist-1-the-patchwork-adaptive-soundtrack-in-fortnite-creative
* https://dev.epicgames.com/documentation/en-us/fortnite-creative/using-patchwork-value-setter-devices-in-fortnite-creative
* https://dev.epicgames.com/documentation/en-us/fortnite/using-patchwork-value-setter-devices-in-fortnite-creative
* https://dev.epicgames.com/documentation/en-us/fortnite/verse-start-05-refactor-and-refine-in-fortnite
Verse source files
- 01-device.verse · device
Turn this into a guided course
Add OFF Value Setter 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.