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?
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.
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’.
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.
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.
Interesting. I have 2 questions regarding this approach:
How do I trigger an event from Game Instance? Event BeginPlay does not seem to be available in a Game Instance.
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?
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?
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.
I made a global function (Blueprint Function Library) that takes a Sound Cue and calls the BP_GameInstance custom event to play the cue
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.