Unreal Engine + Wwise - Managing Music State throughout Multiple Levels
Tags: gamedev, wwise, gameaudio, unrealengine
Published: 2022-01-09 16:06:01

So, for the past few months since my last blog post, I hit a brick wall in my progress because I was stuck with this problem when I finally got the chance to work on the music implementation for my game "Maliksi." This led me to take a rather long break out of near frustration and stress, especially because I wanted to move on to other projects, plus I got busy with other obligations to worry about. Now entering 2022, I'm wanting to finally try to finish this game once and for all!

> Side-note: I'm still in current development, but I really felt the urge to blog this as soon as I found a working solution. 
>So it might be a little messy with my current way, but I wanted to preserve this information for future references.
>There are probably better ways to do this, but I was at least able to get this to work for me. 
>For this purpose, I'm using Unreal Blueprinting.
>Also, I'm no expert in Unreal Engine, as I'm still learning this engine myself too.

---
###Problem to solve:

To play a Wwise music event and control its state when transferring to different levels in Unreal Engine.

---
Coming from a Unity background, I was used to the "DontDestroyOnLoad" feature attached to a game object, where I could let that run even when transferring to different scenes, all until the game closes. From my research high and low, Unreal Engine did not have a component like that.

If you are not sure how to play a Wwise music event into a scene, here is a youtube video for playing an SFX object, but it's essentially the same way for music.
[How To Integrate Wwise With Unreal Engine + Play An Ambiance SFX by Scott Game Sounds](https://youtu.be/TBPvJqE0iJQ)
**Time Stamps**: 
10:20 - Wwise Setup
15:20 - Unreal Engine Setup

For a brief summary, the life cycle of a game scene or level (this applies to both Unity & Unreal Engine), 

1. Game objects & actors are created as soon the level is opened.
2. As soon as the game objects/actors are spawned, they fire their starting function:
- Unity -> Start() or Awake()
- Unreal Engine -> BeginPlay()

![Actor Lifecycle](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/73vqkqctu06qq0cw81q4.png)

What this problem lies is, I want my Wwise music object to be playing. When it transfers to the next level, I want the Wwise music object to seamlessly change to the next track. However, that Wwise music object will get destroyed as soon as I transfer to the next level.

I could just create a new Wwise music object every level and play the music for that level, but I also had a change for vertical orchestration layering. Here is my setup for how I want my music to play.

![Music State Flow](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ae9w2jn19vkqu6g58kws.png)

*Rondalla is Filipino traditional music involving plucked instruments similarly compared to the mandolin.

So going back to an early point I brought up, from Unity "DontDestroyOnLoad" was something I needed to find something that's similar on Unreal Engine. Here were two options I found:

1. Game Instance
2. Persistent Level / Level Streaming

I experimented with both, but which one worked in my favor at least? The Persistent Level choice. You can skip to the Persistent Level section to read about using that one, I wanted to share my experience with the problems I occurred trying out with game instances.

###Game Instance

So what are game instances? These are objects that are created as soon as the game loads, mainly like in it's own storage. You can possibly think of them as static since they can only exist once but its values can be altered.

For example, I use game instances in my game as unlocks, when Malik defeats a boss, she gains a new skill unlocked, which retrieves the game instance to tick that THAT certain skill is now unlock because the particular boss is now defeated. With this in mind, I thought I could use this to save the state of the game object that contains the Wwise music event. However, I realized that you can't save objects or actors onto game instances, only variables with data type values such as int, boolean, string, etc.

Although, I **could** possibly still use the game instance to save and alter the state value, but I ended up not doing it when I tried implementing the 2nd option.

###Persistent Level / Level Streaming

What is a persistent level? It's a level that will be continuously running along with your main levels and maintains any objects contained in that level. For example, this can be applied to maybe having the character UI run throughout the game or certain environment assets. 

###Steps

1 - Create a new empty level, name it anything you want, I named mines "MusicManager".
2 - From your first level opened, mine specifically is "Title", open the levels window.

![Persistent Level Step 1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2uaclmq176ctwvor2dqu.png)

3 - At the top should say "Persistent Level," drop the "MusicManager" level under.

![Persistent Level Step 2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lrg8gggv16pzx7iqwnjk.png)

4 - Right click the "MusicManager" Level in the Windows Level, go through to the drop menu to "Change Streaming Method" and make sure the "Always Loaded" option is checked.

Note: Streaming Mode
- blueprint: when you want to load your level manually, using "load stream level/unload stream level" in the blueprints
- always loaded: as soon as the level loads, it will continuously run until the game ends

![Persistent Level Step 3](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4ge16gsr46jpn5kup1ab.png)

4.5 - On the bottom right, you can switch between managing the Persistent Level or the Music Manager. Make sure to set to Persistent Level when you are ready to play test.

![Persistent Level Step 4](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n5lecn7f0ddaqhaulqki.png)

5 - Create a new actor new blueprint, I called mines "musicObj." Attach an AkGameObject component (or Ak component, I believe either should work). 

Get the current level name, use that as the condition for a switch statement for each of your levels (you can probably create an array too to manage the levels as well).

![Persistent Level Step 5](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c2bwahkik8elthmmolt2.png)

6 - The starting condition will immediately set state to the music state event I have already set up in Wwise and then post event. Make sure that the post event references itself.

7 - As the following conditions, it just has to set state.

7.5 - Note, if your music event ends, such as playing an ending stem but still want to play the next music event, just add another post event after. I did test this and it worked.

![Persistent Level Step 6](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5tlw8yaw5ttbw18xejm3.png)

8 - For each level, I attached the "MusicManager" level on to every level, repeating steps 3 and 4.

Although, I still have not tested yet on retries when my character dies or back tracking in my levels (although for my current design I'm not allowing back tracking). I presume at least with this set up it should allow it to set state and change the music state event when you back track levels too.

Here is also my current set up for the Wwise side as I have the music states set up accordingly.

![WWise Music State](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/us7jctkqx887a0v25u9t.png)

Failed Attempts I tried & problems that occurred:
- I tried to utilize Switch Containers within one of the levels because I wanted to Vertical Orchestration seamless transition loop to work, however, it just did not cooperate with me just resorted to making it as state values.
- Game Instances could not keep the musicObj alive as I tried to retrieve it through each level, but it only can hold variable values and not blueprint actors.
- I tried to manage changing the state on the level blueprints for each level but it meant I had to keep retrieving the musicObj every level that way, and it was not able to fully access and change the state from there.

With this hurdle finally overcome, I feel that I can finally finish this game project once and for all! Thank you for reading and I hope this helps for the game audio folks because it was really difficult for me to find resources on Unreal Engine and Wwise for music, especially when working with multiple levels.

Here is a preview and link to my upcoming solo game "Maliksi." 

![Maliksi by Fabian Fabro](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/99jc9stndkagi6vfga44.png)
[Maliksi Itch.io game link](https://firahfabe.itch.io/maliksi)

Maliksi Main Theme by Fabian Fabro (FirahFabe) Music Preview
Youtube Video:

[![Maliksi Main Theme by Fabian Fabro (FirahFabe)](http://img.youtube.com/vi/9YiceEbgMSI/0.jpg)](http://www.youtube.com/watch?v=9YiceEbgMSI)

Other Video Resources:

[Wwise 201-06 | Using the Music Switch Container by AudioKinetic](https://www.youtube.com/watch?v=829S9ipncOA&t=362s)

[Wwise 101-08 | States by AudioKinetic](https://www.youtube.com/watch?v=pRwBahG4dgY&t=189s)

[Unreal Engine 4 Tutorial - Level Streaming by Ryan Laley](https://www.youtube.com/watch?v=PyMMgWKd5QM&t=316s)

[UE4 - Tutorial - Game Instances by Dean Ashford](https://www.youtube.com/watch?v=ffQ-imKkO4U&t=268s)