I want to alter audio components, specifically by changing their attenuation settings during gameplay. For example I have place a metasound in the world, and want to override it’s attenuation by some set values. In my case I want to set spatialization on so that the sound has a location in the world, but this does not work.
I don’t really have a lot of experience with audio in unreal yet, but this is what I’ve tried to do in C++:
UAudioComponent* AudioComponent = ActorItr->GetAudioComponent();
if (!AudioComponent) continue;
UE_LOG(LogRsap, Log, TEXT("Audiocomponent: %s"), *AudioComponent->GetName())
FSoundAttenuationSettings CustomAttenuation;
CustomAttenuation.bSpatialize = false;
CustomAttenuation.AttenuationShape = EAttenuationShape::Sphere;
CustomAttenuation.AttenuationShapeExtents = FVector(300.f);
CustomAttenuation.FalloffDistance = 150.f;
AudioComponent->bOverrideAttenuation = true;
AudioComponent->AttenuationOverrides = CustomAttenuation;
if (FAudioDevice* AudioDevice = AudioComponent->GetAudioDevice())
{
if (AudioComponent->IsActive())
{
AudioDevice->SendCommandToActiveSounds(AudioComponent->GetAudioComponentID(), [CustomAttenuation](FActiveSound& ActiveSound)
{
ActiveSound.AttenuationSettings = CustomAttenuation;
});
}
}
The part where I call ‘SendCommandToActiveSounds’ is from the UAudioComponent::SetOverrideAttenuation. Could someone explain to me how we are supposed to do this? I’d like to be able to do this without having to create a SoundAttenuation asset in the editor.