Hi. How does one make a sound settings menu? Do I have to do a variable that represents sound volume in all blueprints that use sounds, or maybe I can choose all sounds I have and control them at once somehow? I want to control game sounds/music/menu music sounds sepratately Thanks
As far as I know, modifying game volume still isn’t exposed to Blueprint, so you have to use C++, but you can make a convenient Blueprint node with C++ to make it easier. All you need is a BlueprintFunctionLibrary class with the following functions in it:
CustomBPLibrary.h
// Sets the volume of a sound class Asset
UFUNCTION(BlueprintCallable, Category="Settings|Sound")
static void SetSoundClassVolume(USoundClass* SoundClass, float Volume);
// Gets volume of a sound class Asset
UFUNCTION(BlueprintPure, Category="Settings|Sound")
static float GetSoundClassVolume(const USoundClass* SoundClass);
CustomBPLibrary.cpp
void UCustomBPFunctionLibrary::SetSoundClassVolume(USoundClass* SoundClass, float Volume) {
if(SoundClass) {
SoundClass->Properties.Volume = Volume;
}
}
float UCustomBPFunctionLibrary::GetSoundClassVolume(const USoundClass* SoundClass) {
return SoundClass->Properties.Volume;
}
Then you need to create a Sound Class asset for each volume type (Master, Music, etc.) ideally with Master as the parent of the rest so master volume works correctly. The engine does come with a default set of some common ones like Master and Music, but I recommend making your own for more finite control over the settings. Here’s my setup as an example:
Next, you need to apply a Sound Class to all of your Sound Cues and other Sound Assets that have a Sound Class property. For Sound Cues, you set this in the Sound section of their Details panel. It should be the same or similar for other sound-related Assets that use Sound Classes as well. The volume and pitch multipliers shown here are from the Sound Cue and applied on top of the Sound Class properties, so the final volume coming out of the Cue is ClassVolume * CueVolume.
Now, inside your UMG widget Blueprint, you make a slider for each volume type you want to control. On each slider, in the Details panel you want to find the Events at the bottom and click the big green plus (+) by OnValueChanged.
On this event you use the new BP node you created to set the volume of the related sound class. Conveniently, Sound Class uses a multiplier for volume that is usually in the range of 0 to 1, and sliders use a range of 0 to 1 by default, so you can just plug the Value node direction into Volume on the Set Sound Class Volume node from earlier. So if your slider is for master volume, you use the Master Sound Class, like so:
Now any sound asset that uses the Sound Class you’ve modified should use the same volume. The Get Sound Class Volume is also provided so you can match the slider to the current volume when you load the menu. This should probably be done on Construct, like so:
That should be all you need to get and set the volume at runtime. Do note that this does not carry over between play sessions, so you should save the current volume with GetSoundClassVolume when you save your other game settings, and then apply it with SetSoundClassVolume when you load the settings.
I hope this helps, and let me know if you need any more help with it!
Many thanks. This is perfect, I just need to figure how to add that code to the engine haha. This should be included in the package
Create a SoundMix (lets call it MusicMix), Create a Sound Class (lets call it Music), Set some SoundWave or SoundCue to the Music Class.
When the level starts (on the gamemode or level begin play), call “Push Sound Mix Modifier” and set it to MusicMix.
Now, when you want to change the music volume, use the “Set Sound Mix Class Override” and specify the MusicMix and Music class assets.
You can set a MusicVolume variable on the gameinstance, and just map that to a slider on the menu, so whenever the slider changes, you get/set the variable on the game instance, which calls the SetMixClassOverride node =)