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?
It could do. Just change the start code to this:
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.
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 ):
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.
Will put up a graph in a mo…
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?
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:
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?
Awesome, thanks again mate!
how would i stop the sound on a level if i didn’t want music
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: