How to Call Blueprint After Exiting Level

My main menu is its own level and when the actual game level ends, I want the main menu to open up again. Now, the level opens, but the game level doesn’t seem to completely close as the sound effects still play even though I opened up the menu level. On top of this, the main menu’s music does not play at all.

This is the code I use to establish a win condition and the subsequent behaviors following a victory:

This is the blueprint that I use in the main menu level to control and start/stop the music based on which sub-menu is opened, but when I reopen the main menu level, it is not called for some reason. I am not sure the code here is relevant though, as the problem seems to be with calling a blueprint in general, but here it is:

I specifically want the game level to end, and the menu level to be reopened as normal once it does, music and everything.

1 Like

The open level call will trash everything, including menus. You need to open your menu from inside the menu level ( also do the set input mode there ).

1 Like

I do that and the music blueprint still doesn’t load. And trying to call the music blueprint from the timer blueprint doesn’t work.

I get this error when the Main Menu level reloads:

Now from what I just saw from checking the editor outline, the blueprint does in fact get destroyed and does not spawn in when the main menu is loaded again, and it just clicked that I was doing everything from within the main menu widget, rather than the main menu level blueprint. But I am not sure how to call the menumanager blueprint from the level blueprint, as I was using a widget call the whole time.

What I need to do is create the blueprint every time the level loads.

1 Like

You need the menu manager blueprint in the menu level, then you can call it from the level BP?

Just make sure everything you need to do the menu stuff is in one level.

1 Like

I just physically added the blueprint to the level so it will be there whenever I load the level, but despite calling the instance from the game level the music still doesn’t play.

I get the same runtime error even though I get no errors in the graph because using these nodes in the game level still does not access the menumanager blueprint. I think I am just not properly calling the blueprint in the level event graph, but I thought using the game instance node to do that would work, which it isn’t here.

1 Like

This is happening because this reference is no longer valid. It’s pointing to something that doesn’t exist anymore (wiped when you changed level). This is what access none means.

You need to get a fresh reference to blueprint in question when you change the level. It will spawn in it’s default state. Try using get actor of class and save it in your instance, then see if it will work.

What you could also do, is save all relevant information in a struct inside your game instance before you change the level, then change the level, spawn new actor (YourManaagerClass) and set what you need to set. Also doing it on Begin Play in level blueprint might not be the best thing to do, since you can never guarantee when will this Event Fire off. Most likely it will fire before your get your reference working, so the error will still be there.
Have you tried adding some delay, could be a fraction of a second that you need. Try experimenting with that.

You will not get compile error because “technically” there is nothing wrong with it, apart from your reference not being valid (at the time of call).

1 Like

Did you set your game instance in the project settings?

1 Like

So I managed to get the music playing by using Get All Actors of Class and attach a function from my game instance to it, but the problem is that I need to reference the whole menumanager blueprint because I have multiple songs for different submenus that have to be switched between. Here is what I did, which is still in the level blueprint for now as I just want to make sure it works somewhere before trying to implement it elsewhere more complicated with code:

Yes, and works fine when loading up the level the first time. The issue is just calling the blueprint it’s connected to, or maybe the game instance itself?

And to clarify, I still cannot get the reference for the whole blueprint to work. I’m only able to reference a single function from the blueprint but I will keep trying today.

1 Like

Maaan I still can’t figure this out. I tried using a BPI, the instance, and the game level but I can’t get the blueprint to be referenced. But for some reason I can get individual functions within the game instance to run, though that does not help as my menu does not just have an individual song. Dang.

And in the reloaded level, I can see the menumanager blueprint object clearly spawned in, but I just cannot figure out how to reference it like my main menu widget does. I know it’s something small that I’m missing. It always is.

1 Like

I thought about it for a bit longer and here is my suggestion. I believe you are using music manager to play your background music only? If so, you could remove the actor with audio components altogether and put everything in your music manager inside game instance and use level blueprint to make call to a function inside said game instance to play the right music. You would control which music to play with an enum, or you could specify which music to play directly, but I believe using enumerator would be a bit cleaner solution.

Here’s how:

In your game instance, create a function “Play Background Music” and add a Play Sound 2D node, make sure “UISound” box is checked.
Grab and drag Sound pin from the node onto the black field on a function node, this will create input of the right type. We will be passing appropriate music file to play in another function.

Note: I believe Play Sound 2D is level independent and should persist between levels, but I haven’t actually tested it. :sloth:

For the next step we will need to create an enumerator. You can create one in content browser under Blueprint → Enumeration. I called mine E_MusicManager. Inside add some fields and give then names that work for you, add as many as you need/want.

Finally, create another function (also in game instance), let’s call it Music Manager. Add an input pin of type E_MusicManager (enumeration created above), then add a sequence node. Pull of the input pin on the function and find switch (your enum name should match it).
From pin 1 on sequence call the first function we created. Drag of Sound to Play input and promote it to a local variable. Now back to pin 0 on sequence add a set node for each of your switch statement execution path.

This is where you would set your actual music file to be played. What music will be played for each level will be decided in Level Blueprint(s) themselves.

Now in each of your Level Blueprints you would have a call to Music Manager. Something like this maybe. Too lazy to do interface for it, but hey, don’t make me stop you. :sloth:

I tested it and it works just fine. Let me know if this is what you were aiming for, or throw me a curve ball. :baseball:

1 Like

Maybe take a look at @DonBusso here, I haven’t read it yet, but it looks sound…

2 Likes

You did not have to go through all of that on my account, but thank you for providing this information. So, your solution works, though for a different method of switching menus and music. The current way that I operate my sub menus is purely through widget interface switching, though I think you may have unintentionally showed me a better way to run my menus.

I am going to experiment with using levels to run my menus, as the sub menus I need the music for are for different game modes entirely. So, I may have just learned how to address this problem with a method I didn’t even know I needed, but I will see when I try to do this. I’ll mark the enumerator comment as a solution since it seems to solve the problem under a different method, but it currently seems to work nonetheless haha

Thank you both

2 Likes

Awesome, happy to help. Would be great to hear back on what you ended up going with. Also if you need additional help or different point of view, don’t be shy to reach out.

PS. I try to provide detailed information for future reference, mostly for people that are just starting out. :slight_smile:

1 Like

I created different levels for the menus so now I get the music that I want with the method you provided, and I think doing this also gives me more creative freedom since I can add things to individual levels.

1 Like