In this lesson, you will learn how to use C++ to dynamically manage USoundConcurrency settings, ensuring your game remains performant by prioritizing critical audio during chaotic scenes.
The Problem: Audio Overload
In complex environments—such as a large-scale battle—triggering hundreds of simultaneous MetaSounds can lead to CPU spikes and digital clipping. While Sound Concurrency assets can be assigned in the Editor, hard-coding these limits often lacks the flexibility needed for dynamic gameplay. By managing concurrency via C++, you can adjust how many sounds of a specific "type" play based on gameplay state or performance telemetry.
Implementing C++ Concurrency Logic
To manage this, we interact with the USoundConcurrency object. We can check current playback counts or modify properties dynamically. If we want to prioritize essential audio (like dialogue or player feedback) over background noise, we can use FActiveSound or manipulate the concurrency settings directly on the UAudioComponent.
Here is how you might apply concurrency logic when spawning a sound:
void ACombatManager::PlayExplosion(USoundBase* Sound, FVector Location, USoundConcurrency* ConcurrencySettings)
{
if (Sound && ConcurrencySettings)
{
UAudioComponent* AC = UGameplayStatics::SpawnSoundAtLocation(this, Sound, Location);
// Dynamically assign concurrency to enforce limits per-instance
if (AC)
{
AC->ConcurrencySettings = ConcurrencySettings;
// If the voice count is too high, the audio engine will now
// resolve based on the settings (e.g., stopping the oldest sound)
AC->Play();
}
}
}
Strategic Prioritization
The magic happens within the USoundConcurrency settings. By setting the Resolution Rule to Stop Oldest or Prevent New, you ensure that the most important sounds persist. In C++, you can conditionally swap these assets based on your game’s "Density Level." For instance, if FrameTime exceeds a certain threshold, you might programmatically swap your default concurrency asset for one with a much stricter Max Count to preserve the audio buffer and prevent clipping.
By centralizing this in C++, you move beyond static assets and create an audio system that breathes with the performance demands of your game world.
Key Takeaway: Programmatically controlling Sound Concurrency via C++ allows you to enforce strict audio budgets in real-time, preventing voice-stealing issues and CPU bottlenecks during high-density gameplay.