Keep music playing while switching levels

Hi,

I want to play a music track on the background of my game, and since my levels are pretty short it would be nice if the music didn’t restart every time a new level is loaded. How would I approach this?

Currently I load new levels with the following code:

GEngine->GetFirstLocalPlayerController(GWorld)->ConsoleCommand(TEXT("Open ") + NexLevel, true);

And I play the music using an ambient sound actor in the level.

Thanks.

Hey DuckerDuck -

You should setup a Persistent Level which plays all your sound then it will always be playing even when you are unloading and loading another.

Thank You

Eric Ketchum

I’m not sure how to implement a Persistent level, do I create an empty level where I only add an ambient sound actor and load my menu level from there?
Does this mean I need to setup level steaming for each of my levels? (via blueprints)

Hey DuckerDuck -

You can go into the Levels Window in the Editor and add map file together under a persistent Map. Make sure that you remove anything from the Persistent level that you do not want in your game. You can then control the level streaming using either blueprint or code. Essentially what you will be doing is loading the persistent level at the beginning of your game and then loading and unloading the other maps as you need.

Eric Ketchum

Ok, I’ve been trying all day but unreal just seems to crash with the following error:

So I created a new map(lets call it base), completly empty except for the ambiant sound actor. In the level window i’ve added all my levels and turned them invisible. I’ve set my mainmenu map to load on startup and be visible. In the base level blueprint I have the following for testing purposes:

Now, when I run this it seems to crash the unreal editor on the unload streaming level node with the error above. (The first level is loaded correctly, but the mainmenu actors are still loaded, which is why I want to unload them.)

Hey DuckerDuck -

I think that this may be an error created by using both C++ and Blueprints in the Level Streaming process. And to clarify from your above image you have three levels referenced here. A Base level (base.umap), Minimal_Default.umap and MainMenu.umap Is that correct?

Thank You

Eric Ketchum

Are you using UMG, by any chance? If so, you need to delete any viewport widgets before streaming or moving between levels (that is, unless Epic fixed that already)

I think that this may be an error created by using both C++ and Blueprints in the Level Streaming process.

As far as I know I’m not loading any levels in C++ anymore.

And to clarify from your above image you have three levels referenced here. A Base level (base.umap), Minimal_Default.umap and MainMenu.umap Is that correct?

Correct, Minimal_Default is really the first level which I havent renamed yet, and MainMenu is just a looping Matinee scene that acts as a background for the title screen.

Nope no UMG, just a hardcoded HUD class.

Ah, I think you have told me your problem. You will need to stop the looping Matinee before you unload the level that is playing the matinee.

Try that and let me know if you are getting the same crash.

Thank You

Eric Ketchum

Disabled Matinee actor, worked on first try thanks!

This seems like a rather specific problem, is this a bug or should I have known about this from somewhere?

Hey -

It’s not really a bug so much as the engine being told to run something and unload from memory at the same time.

Thank You

Eric Ketchum

Can the continuous music play be done without level streaming, using just open level function?
I’m doing all savegame and level switches in HUD blueprint.

Hey -

Everytime the level start or a function that would call a level start, your music will be restarted as if it just began playing.