Continuous Music through Level Change and Restart

Thanks! This code is getting somewhere.

I’m noticing that if the level restarts, it seems to trigger the music again, so that multiple instances of the track are playing at once. Are you getting this as well with your code?

EDIT: It only happens on Level 1, which makes sense as that is the Level that has the Blueprint that triggers the PlaySound function. Should some sort of branch be used here to stop the music from being triggered again if the level restarts?

3 Likes

It could do. Just change the start code to this:

1 Like

Hi all

I have been looking online and have been trying to implement this and I’m getting absolutely no luck.

I’m trying to get a sound file (music track) to play and continue to play through level changes and when the level restarts i.e. when the level restarts, the music should not restart but should continue playing. I have been trying with various things, including persistence levels, but I’m not getting anywhere.

Does anyone have a working solution for this?

Attached is my Restart Level code, in case that may be causing issues with persistence levels.

Thank you in advance for any assistance.

Cheers
Matt

1 Like

You certainly can do this with level streaming, play the music in the persistent.

I thought there was a simpler way, but…

EDIT: Ok, yeah, you have to play it from the game instance. It also has to be assigned to a variable:

In the level BP ( of the first level ):

You also need to specify to use your game instance in the ‘maps and modes’ section of the project settings.

AND, you need ( in the game instance ):

4 Likes

This is fantastic. Thank you very much - you have saved me lots of time and this is a such a great clean solution. I have marked this question as answered.

There is one final element I wish to set up, and that is to put into a future Level the capability to change the music that is playing. I have taken a quick stab at it in the GameInstance (code below) and it is not working for obvious reasons. However, I’m a little unsure as to what is the cleanest way to achieve this.

Would you happen to know a solution to this one?

My thoughts are that a good flow would be for the Level Blueprint to trigger ‘Music2’, which is in the Game Instance. However, I have to make ‘Music2’ reset the sound in ‘PlayMusic’.

Cheers
Matt

A slight variation on the ‘play music’ custom event.

Instead of just checking if the reference is valid, it can check if the specified piece is already playing. If not, then stop anything that is playing and play the new piece instead.

Will put up a graph in a mo…

boom:

2 Likes

This is extremely useful and so easy to work with. Thank you again!

Another question has come to my mind - what is a clean way to ensure that the desired music plays when the player loads the game and starts from a Level that is not the first Level that triggers the desired music?

One solution would be to put this code in every Level Blueprint, and modify ‘Sound to Play’ to suit (screenshot below).

With this solution, multiple Levels would have to have the exact same code. For instance, Levels 1- 10 require the same music, so the exact same code should be placed into the Level Blueprints for Levels 1-10.

The only issue I can foresee with this is that if I have to rearrange levels, then I will need to double check each Level Blueprint to ensure the appropriate sound is triggered. I’m wondering if there’s a cleaner way to achieve this.

What are your thoughts?

Cheers
Matt

Why not start it from the game instance? That way it will always play regardless of level…

Interesting. I have 2 questions regarding this approach:

  1. How do I trigger an event from Game Instance? Event BeginPlay does not seem to be available in a Game Instance.

  2. How can I control which sound file plays for which level if the PlaySound function is in the Game Instance? For instance, how can I ensure that Levels 1-10 play SoundFile1, and Levels 11-20 play SoundFile2?

  1. In the GI, it’s called Event Init.

  2. If you want different music for each level, then do what you’re doing now - ie - make a call to the GI from the level BP. That’s the best place for it :slight_smile:

Awesome, thanks again mate!

how would i stop the sound on a level if i didn’t want music

Thanks :slight_smile:

1 Like

Hello, I have run into an issue, I am basically attempting the same thing, and have been following along, but for some reason whenever I tick the persist between level transitions box the sound mutes itself. When I check it says that the sound is playing and that the volume multiplier is set to 1, but there is no sound playing, how do I fix this?

Update: I tried this out of desperation and for some godforsaken reason this works:

1 Like

This is the best answer; I had to unpack it a little to understand it for Unreal 5.3:

  1. Import your soundtracks as a audio wave files and stash in your content (I use an Audio folder).

  2. Create a Sound Cues. In each one set the Class to Music, and drop in the soundtrack WAV, making to to make it loop

  3. Create a blueprint of type Game Instance, e.g. BP_GameInstance

  1. Change the game instance class: Project Settings > Maps & Modes > search for “game instance” > change to your BP_GameInstance

  2. Edit the BP_GameInstance blueprint

image

5b) Create variables to hold the currently playing sound component, and the last sound cue. Bonus: add a variable for the audioFadeTime.

5c) I went with a Custom Event called “PlaySoundtrack”. It takes a “new sound cue”. If the current playing sound is the same as what was last playing, it does nothing. Otherwise it fades out the current soundtrack and then spawns a new soundtrack.

  1. I made a global function (Blueprint Function Library) that takes a Sound Cue and calls the BP_GameInstance custom event to play the cue

image

  1. Finally, on each level/map, it’s as easy as going to the event graph and on Event BeginPlay you can call the global PlaySoundtrack function. That way whatever level you play it will play the right soundtrack, but it won’t change it if it’s the same song.