How to make an AmbientSound at runtime?

In my game the player can make buildings, and I want to associate an ambient sound with the buildings.

The problem is that there’s no AmbientSound component to attach to an actor. The regular Audio component that you can attach to an actor doesn’t act like an ambient sound (if you leave the sound range and come back in it won’t be playing if it’s between loops).

So I realized AmbientSound is actually an actor itself, not a component. Maybe I could make one at runtime and just place it at the location of the building actor.

So on the startup event of the building actor I tried to spawn actor from class AmbientSound. Then I use SetSound node to say which sound, and then I have tried both Play node and Set Active node, but in either case the sound doesn’t play…

Am I missing something else I would need to do here?

That’s a perfectly valid way of doing it, there must be a little hitch somewhere.

Another way is, inside the blueprint BP:

You need to set the attenuation settings to get it to fade in the distance.

PS: Put the actor location in the location pin

Works perfectly thanks!

Please check for me how the Sound or Sound Cue uasset file’s virtualization property is set. Try setting it to “Play when silent”.

Worked perfectly! The sound now continues playing when it realizes the player is in range again!

I think the AudioComponent itself would be fine for this situation, I use them myself for a music player I made in c++. The problem seems to be that the AudioComponent stops playing the sound if you are at a distance where you can not hear it? Which makes me wonder if this is default behavior for the AudioComponent when its volume reached 0.
Please check for me how the Sound or Sound Cue uasset file’s virtualization property is set. Try setting it to “Play when silent”