[4.11.2] Spawn Sound Attached

“Spawn Sound Attached” is a “fire and forget” sound. Why?

The “Play Sound x” are supposed to be “fire and forget” sounds, whereas the “Spawn Sound x” are supposed to return an audio component reference to Play, Stop and Destroy it manually.

“Spawn Sound Attached” also returns an audio component reference, but yet it gets automatically destroyed after a while.

Not only that, but if the sound is a loop, the sound will never stop, it will keep on looping.

Anyway, is the name and/or functionality of “Spawn Sound Attached” incorrect or bugged?

Edit: The problem was caused by running Play on it, apparently that causes it to bug since it automatically plays when spawned.

Not 100% sure I follow what you’re saying. Spawn* functions indeed return audio components and are not intended to be fire and forget.

There was a bug I fixed recently where if you immediately call FadeIn on the returned audio component of SpawnSoundAttached (or any audio component), it would destroy the original audio component and create a new one. Other Audio Component BP functions that cause the sound to replay will also have the same issue. Unfortunately, it wasn’t fixed in time for 4.11.2 – if you’re having issues with this and can patch UE4, the fix is simple.

In UAudioComponent::PlaybackCompleted, add the following bool check before calling DestroyComponent():

	if (bAutoDestroy && !bIsActive)
	{
		DestroyComponent();
	}

If you mouse-over a SpawnSoundAttached blueprint, it still says “fire and forget”.

Anyway, the bug I had was caused by calling Play on it.

In steps:

  • I spawned the sound component (with SpawnSoundAttached)
  • then I called Play on it
  • then I had a Delay node (of 1-2 seconds)
  • and then I called Stop on it

When I do this, the sound component will already be destroyed (IsValid returning false) before it reached the Stop blueprint.

I had the sound asset in a variable by the way, from the moment it was spawned, until the moment I called Stop on it. So it wasn’t garbage collection that removed/destroyed it (if that is even possible with components).

Anyway, besides that, the sound will then also keep playing forever (my sound asset was a loop by the way), even though the sound component is destroyed at this point.

I saw the exact same problem. My workaround is to create a blueprint with an audio component and spawn those. This way the audio component doens’t get auto destroyed.

This is the error note when using an audio component from the ‘SpawnSoundAtLocation’ node:

Error note: Error Blueprint Runtime Error: Attempted to access AudioComponent_228 via property CallFunc_SpawnSoundAtLocation_ReturnValue, but AudioComponent_228 is pending kill from function: ‘ExecuteUbergraph_BP_River’ from node: Set in graph: EventGraph in object: BP_River

My workaround was to not call Play on it, because it already starts playing when you spawn it.

If you call Play on an already playing spawned sound component, it will cause bugs (probably because the sound is being played twice, and after the first play it will automatically remove/destroy it, but then the second play is still going on and, with looping sounds, the sound won’t ever stop).

Yes, this is what I was saying below is the issue. The fix, as detailed will work because we track how many sounds are playing on an audio component and only flag it as inactive if the number of sounds playing is 0. Thus, if you check bIsActive before destroying the component it’ll prevent the issue. Not calling Play will also bypass the problem but is not an acceptable general-case solution.

@Minus_Kelvin, The bug you fixed appears to have suffered a relapse. On 4.12 it was fine, but as of 4.13.1 if you Fade In an audio component immediately after creating it, it gets destroyed.

This is probably because of 4.13’s removal of the “play at zero volume” by default and made it as an opt-in feature for sounds. For sounds where you want it NOT to get stopped at zero-volume, make sure you enable “bVirtualizeWhenSilent” on the USoundWave. This will make it so the sound isn’t immediately stopped if it plays at 0 volume.

Thanks for the info Minus_Kelvin, now I know how to prevent my game music from stopping when the user decreased the in game sound volume to zero.