Music that changes based on events

So, I have music available for multiple events that happen in game. I don’t know exactly how to program it so that when an event happens, the music changes to a new song without overlapping.

For example, when my character dies, I want the music to completely stop and play the song I have for when they die.

Or when a guard is alerted, switch songs to an alerted theme, and when all the alerted guards are dead, the first song that was playing starts playing again.

Is there anyone who knows how to do that?

Absolutely! You can do this in a variety of ways, but I will show you one way to achieve this.

If you open your character blueprint, you can add components. In your case you want to add the Audio component so that you can play music from it.
bild

The second step would be to add music you want to be in the game by default when starting the game.

Now for the coding:
To play a sound you need to trigger them by events. The Event BeginPlay triggers a sound when the level starts, and I use the audio component and a play node to play the default sound set in the audio component:
bild

I will now setup a custom event that will stop the music, change the music, and play the new music:

I can now call the custom event function and add it anywhere where I see fit. This is done like this, right-click and search for the event you created. The red marking around the node is to showcase how it looks once you add it:

If you want to do this for a lot of events, creating a function with the same principle would be great! That can be done here:

2 Likes

Thank you! This is exactly what I was looking for.