Overlapping Audio called on in AI Controller BP(UE4 - v4.26.2)

Hello everyone, I am developing a game where its going to be a simple FPS game; I have no C++ knowledge but I’ve worked with UE4 as a level designer for 2 years before going in on my own project to teach myself more about blueprint scripting specifically.

Question: In my AI controller BP (which the basic enemy type uses as its controller), I have an audio component that plays the battle music when it sees an enemy with PawnSense component.

I have an audio sound cue that plays the ambient music in the level, the issue I am having is that when the level plays of course the ambient music plays but it overlaps on the battle music, I know I need to somehow get a reference to the SoundCue that is in the persistant level itself but I am not sure how to do that, I have looked around and even tried dragging and dropping the soundcue from the world outliner but that just creates an add audio component.

Do I need two audio components inside the AIController BP so that I can tell it which music to fade in and out? This is all based on the PawnSense sight component, I have a bool set up that says if the the pawn sees the player, play the battle music, if not fade out over duration of 5 seconds.

Any ideas?

P.S. I do have another related question, in my AIControllerBP it says that when the pawn sees the player to play the music and to fade out if it doesn’t. However when I get multiple pawns who all see the player at the same time it plays the music for each pawn. I am not sure how to fix this one at all currently.

Here is my current blueprint setup for the AIControllerBP:

Thank you all for your help! Take care!



Still no luck fixing the overlapping audio, any feedback would be much appreciated.

I think it has to do with switching the ambient and battle tracks in the sound cue bp but I am not sure.

Might it be better to have a blueprint that handles playing the audio that your ai calls? That way you could do a check in the audio BP you create to see if the battle music is still playing and also handle the logic for starting/stopping the ambient audio at the right times.

I’ve not had much experience with handling sounds but this seems like a logical step.

I can try and make something in blueprint form if you’d like an example of what I mean.

That’s probably the way to go, this is my first time trying to play with this particular system. Do you mean to create an empty bp and then fill it with all the music logic? Or would that more or less be done with the ambient sound actor/audio cue?

If you can provide an example that would be great, thank you!

Sorry for not responding sooner, my idea became a bit more involved than I would have liked and I’ve done my best to explain it below.

Note that I’m not great at making things work well performance wise, however, the below code I believe works for your use case/issues.

Step 1: Create a SoundCue in the content browser.
a. In the SoundCue add a CrossFade by Param node and plug into this your ambient sound (pin 1) and your battle sound (pin 2).

I followed this post to figure out how CrossFade works and came up with the following settings. Note that I called the CrossFade param name ‘Duration’:

CF Settings

Step 2:
Create an actor Blueprint that will handle your Sound logic. I’ve called mine MasterSound_BP and drag this into your level somewhere.

Back in your MasterSound_BP class set the BeginPlay event like so:

Click on Add Audio Component and set the sound to the SoundCue created above.

If you run your game you should then find that the ambient music (whatever is in pin1 of the CrossFade) is playing automatically.

Step 3:
If you’ve not setup a Blackboard for your AI you can create one in the content browser by Right Clicking->AI->Blackboard.

In your Blackboard create a new Key with the following settings:

The Base Class is whatever you called the class created in step 2.

Step 4.
Replicate the following in your AI Controller class.

Here we set the Blackboard to use (Blackboard Asset is whatever you called it in Step 3).
We then do a check for if the Blackboard Key MasterSound is already set.
If True: Do Nothing
If False: Find the MasterSound_BP we added to the level and set this as the value of the key.

Because Instance Synced is set to true in the Blackboard key, the logic above results in us only having to run Get All Actors of Class once.

Step 5.
Back in the MasterSound_BP class create the following custom events:

Here we’re creating a timeline between 0 and 1 and interp between 0 and 1 at an interp speed of 0.001. (Note that someone with more experience in timelines would probably cringe at the above description and implementation as I’ve probably not explained it or implemented it right, but it does work).
This value is then fed into the SoundCue CrossFade parameter ‘Duration’ to crossfade between the ambient and battle music.

The check for the number of AI that found the player is done to stop the AI keep calling the function and the timeline keep being restarted. I.e. if one AI saw the player and then 0.1 seconds later another AI saw the player, without this check in place the timeline would run twice from PawnSeen.

There may be a better way of handling this but I couldn’t come up with a different solution.

You may also encounter a bug if PawnUnSeen is called and the count is set to 0 but a split second later PawnSeen is called (and vice versa), it will cause what will sound like a skip in the fade. This is only theory as I couldn’t test this. You’d need to come up with some additional logic to only start the TimeLine again once the previous TimeLine has finished.

The FadeInBattleTrack is just a timeline (Right Click → Search for TimeLine->Add TimeLine).

  1. You need to press the f+ button to create a new float track
  2. Change NewTrack_0 to a more meaningful name
  3. Make the Length 1.00
  4. Right Click in the float track and select Add Key to CurveFloat_(some number)
  5. Set the time to 0 and the value to 0
  6. Add another key and set the time to 1 and the value to 1

Step 6.
Back in your AIController class create a flow that’s similar to the following:

Let me know if there’s any questions on what the logic is doing here.
You may find that a retriggerable delay of 1.0 seconds is too short, so I’d suggest changing this value.

Let me know if there’s any questions about the above.

Hello again! So I hooked up everything the way you showed, and the sound cue plays the ambient track but when seen it will not play the battle track, I am at a bit of a loss and I also have been getting this runtime error:

Thank you so much for your reply! I feel like this I am very close to actually getting this to work, just something is causing it to only play the ambient sound track and it never switches to the battle track it just loops. I am probably missing something very obvious I just can’t see what it could be atm.

Thanks again for all your help!

P.S. Currently as a new user I cannot post more than 1 embedded media item so if you’d like me to share the BP I wrote does anyone know how I can share that without making multiple replies?

EDIT: I went through the tutorial again and when do you use the CrossFade param Duration? I cant see it being called or used anywhere.

EDIT 2: Okay so in the Use Blackboard node in the AI Controller I didn’t have that set, so I set that and I changed the interp to the correct 1 and 0.001. Still the ambient track plays (track 1) but track 2 will not play at all once the AI spots the player it just gives chase, I am not quite sure whats going on here.

What I do notice is that when playing the game and viewing the MasterSound_BP in the BP editor is that the battle music timeline never triggers and remains paused even when seen by the pawn. Not sure if that is helpful or not though.

Did you fix your runtime error in Edit 2?
In regards to why there’s no crossfade, make sure that in step 5, in the set float param, you have duration written there.

You could try adding a breakpoint at the start of the On Seen Pawn node (right click a node and add breakpoint), then when you run your game, if the player is seen the game should freeze and you’ll be able to go through the execution path from the breakpoint onwards (pressing F11 i believe or use the step into button). Use this to check that the logic is executing as expected.

I did fix the runtime error , and I did eventually notice that I messed up step five in terms of setting the float param, but I fixed that. The enemy now sees and chases the player but the music doesn’t change for some reason. I’m going to try breakpointing the on see pawn and see what happens.

So when the pawn sees the player it goes into Cast to ThirdPersonBP, and then the branch with the condition “CanSeePlayer”, and then the sequence pin 1, then Set can see player bool True, then AI move to, then branch (where false is nothing), and then it backtracks back to the Sequence node pin 2, goes into the retrigerable delay, then it goes back to OnSeePawn node and it loops in that order permanently.

EDIT: Here is a picture of whats going on visually.

One thing I have seen is your re-triggerable delay is set to 0.4. However I believe the frequency of PawnSensing is 0.5 by default (set sensing interval). So it will be resetting the variable value back to false before the next On See Pawn event call. What happens if you set the retriggerable delay to a value above the Set Sensing Interval value.

After a lot of fixing minor errors on my end and a little tinkering it works! Thank you so much for your response!