Skip to main content

12 - Audio

A small UActorComponent audio manager plus a standalone helper actor: PlaySound2D, a USoundBase slot, and volume stepping.

Full source (manager component)​

include "CoreMinimal.h"
include "Components/ActorComponent.h"
include "Sound/SoundBase.h"

uclass UExampleAudioManager : UActorComponent meta: api=EXAMPLES_API
{
prop public USoundBase* KickSound EditAnywhere BlueprintReadWrite category="Audio"
prop public float Volume EditAnywhere BlueprintReadWrite category="Audio" default=1.0f

method public void PlayKick() {
if (KickSound)
{
UGameplayStatics::PlaySound2D(this, KickSound, Volume);
}
}

method public void IncreaseVolume(float Amount) {
Volume = FMath::Clamp(Volume + Amount, 0.0f, 1.0f);
}
}

Line by line​

  • uclass UExampleAudioManager : UActorComponent - a component you add to an actor to give it audio behavior.
  • prop public USoundBase* KickSound ... - a sound asset slot, assignable in the Details panel.
  • prop public float Volume ... default=1.0f - master volume for this manager.
  • PlayKick() - if a sound is set, plays it 2D at the current volume via UGameplayStatics::PlaySound2D.
  • IncreaseVolume(float Amount) - steps volume, clamped to [0, 1] with FMath::Clamp.

The data flow​

Designers drop this component on any actor, assign the sound asset, and call PlayKick() / IncreaseVolume() from Blueprint or C++. The sound data stays in the asset; the component only orchestrates playback.

Volume clamp​

FMath::Clamp(Volume + Amount, 0.0f, 1.0f) is the idiomatic way to prevent volume drifting past 0 or 1. It's exactly the kind of body you can write freely - TPScript passes it through and the emitted .cpp matches.

Next: 13 - Camera.