Hi guys. Have a trouble here.
Needed to create dynamic massive of UAudioComponent pointers, so can’t declare it as a UPROPERTY.
How can I stop the auto activetion and, though stop auto playing my sounds?
Here is like my massive is declared:
UAudioComponent **SoundComponent;
This is old but shows up in Google searches, so here is the answer. This code attaches any number of sounds to an Actor and plays/stops them with by passing in the name.
/**
* List of sound cues played by this actor.
*/
TMap<FName, UAudioComponent*> _soundCues;
/**
* Add a sound component to actor.
* Actor manages sound cues for all components in a map.
*/
void AInteractiveActor::AddSoundComponent(FName soundName, const char* cueAssetNameAndPath)
{
UAudioComponent* newCue = CreateDefaultSubobject<UAudioComponent>(soundName);
ConstructorHelpers::FObjectFinder<USoundCue> cueAsset(ANSI_TO_TCHAR(cueAssetNameAndPath));
if (cueAsset.Object != NULL)
{
newCue->AttachTo(RootComponent);
newCue->bAutoActivate = false;
newCue->SetSound(cueAsset.Object);
_soundCues.Add(soundName, newCue);
}
else
{
UE_LOG(MCS, Error, TEXT("%s: add sound component failed!"), *GetUniqueName());
}
}
/**
* Play a sound cue.
*/
void AInteractiveActor::PlaySound(FName cueName)
{
// find sound in cue list
UAudioComponent* cue = _soundCues.FindRef(cueName);
if (cue && !cue->IsPlaying())
{
cue->Play();
}
}
/**
* Stop a sound cue being played.
*/
void AInteractiveActor::StopSound(FName cueName)
{
// find sound in cue list
UAudioComponent* cue = _soundCues.FindRef(cueName);
if (cue && cue->IsPlaying())
{
cue->Stop();
}
}
// add audio component in your Actor constructor
AddSoundComponent("alarm", "/Game/Audio/Test/alert_Cue.alert_Cue");
// play or stop sounds at any time in game like this
PlaySound("alarm");
StopSound("alarm");