Way to control game sounds with sliders?

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.

230596-set-sound-class.png

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.

230553-slider-events.png

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:

230578-set-sound-class-volume.png

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:

230594-get-sound-class-volume.png

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!

1 Like