In this short tutorial we are going to add a volume dialog to set the volume of the music and the SFX effects independently
In the most basic volume control each sound type has a slide control to adjust their volume, moving the bar to one side the volume will be increased and decreased moving it to the other side.
But, before starting with the UI, we need to understand some audio concepts of UE4.
The audio system in Unreal Engine 4 is made up of several components, each working together to produce the audio experience for players.
The first step is to import the file sound. We can use the same button used to import other assets.
Importing a sound file into the editor generates a Sound Wave asset that can be dropped directly into a level or can be used to create a Sound Cue and edited inside the Sound Cue Editor.
Sound Cue
Sound Cues are composite sounds that allow us to modify the behavior of audio playback, combine audio effects, and apply audio modifiers with Sound Nodes to alter the final output. For more information, refer to the Sound Cue Editor page.
Sound Class
Sound Classes are a collection of properties that can be applied to a number of Sound assets. We can set a sound class for background music, other for sfx sounds, other for dialogs, and establish a hierarchical relation between them.
Hierarchies can be created by adding Child Classes, which will allow us to pass down only specified properties from the parent class to children classes. We can connect class together inside the Sound Class Editor, which shares a similar node-based interface as seen in the Sound Cue Editor.
For our example we are going to create the next hierarchy:
- Master
- Music
- SFX
Sound Mix
Sound Mixes allow us to set the EQ Settings (Equalizer Settings) and modify Volume and Pitch properties of Sound Classes.
Multiple Sound Mixes can be active at the same time, all contributing to the overall audio effect. We can Push (Activate) or Pop (Deactivate) Sound Mixes directly inside a Blueprint with the Push Sound Mix Modifier and Pop Sound Mix Modifier nodes.
To set the volume of our class dynamically we need to use the Set Sound Mix Class Override node. It can modify and set an active Sound Mix to use any Sound Class you have, and interpolate between its current Sound Class and the new Sound Class over time.
Note that if the Sound Mix is not active (has not been pushed) we need to use the Push Sound Mix Modifier node to do the change effective. In successive call we can override the sound mix using only the Set Sound Mix Class Override node.
Fade in Time specify how quickly to transition from current volume to the new volume. For an instant change we will set it to zero.
Blueprint sample
For this sample we are going to import 2 audio files, one for background music and the other for a SFX effect.
Three sound classes SC_Master, SC_Music and SC_SFX, with the previous hierarchical relation
We can create a Sound cue for each audio track and set the related sound class in their Sound section
and two Sound Mixes to change the volume of the sound classes independently.
- SM_Music
- SM_SFX
We can add the background music dropping the cue inside the level or adding an audio component and choosing the cue in the Sound drop combobox.
We can interact with this component using audio nodes like Play, Stop, Set Pause
We can add two variable to our game class to store the volume values.
We must save the slider values of the volume control to a savegame and initialize their values the next time we start the game. We load the volume values in our variables and set the Mixes volume values at the same time that we override the mix of the appropriate sound class using the Set Sound Mix Class Override node. We need to Push the mixes at this point to activate it too.
The options dialog consist in two slider controls
We need to define their OnMouseCaptureEnd/OnControllerCaptureEnd and OnValueChange events.
We want to change the music background while the user is moving the thumb over the bar so we need to set a Sound mix class override node in the OnValueChanged event. Note that we don’t need to push again the Sound mix, we pushed it in the game level initialization. When the user release the thumb we can save our variable to the physical file for the next start.
The SFX slider has a similar implementation but we want to emit a sound when the user release the thumb to check the final choose volume. To play this sound we can use the Play Sound 2D node with the sfx cue.
We can’t forget to initialize the slider values when the widget is added to the viewport.
And that’s all!
You may also like:
Support this blog!
For the past year I've been dedicating more of my time to the creation of tutorials, mainly about game development. If you think these posts have either helped or inspired you, please consider supporting this blog. Thank you so much for your contribution!