Unreal Dynamic Music System from a Mowjera Export

advanced · about 45 minutes · checked

You will add the exported music component to an Unreal project and switch states from C++ or Blueprint.

All lessons

A simple Unreal dynamic music system from a Mowjera export has three parts: the UMowjeraComponent class in Mowjera.h and Mowjera.cpp, your looping sounds imported as SoundWave assets, and a call to SetGameMusicState when the game changes. The component crossfades between states.

The authors of this guide have not compiled or tested these files inside Unreal Engine. The steps follow the generated source code.

Before you start

  • Export your game music bundle and unzip it. The editor button exports WAV, which Unreal imports.
  • You need an Unreal project that can compile C++.
  • A SoundWave is Unreal's asset type for an imported sound file.
Export panel with 1 the Export Bundle button
Export panel with 1 the Export Bundle button

Step 1: add the source files

Copy unreal/Mowjera.h and unreal/Mowjera.cpp into your game module's Source folder, then build the project.

Step 2: import the sounds

  1. Import every file from audio/ into the Content Browser. See Epic's audio import page.
  2. Open each SoundWave and turn on Looping.
  3. Set Virtualization Mode to Play When Silent. The component keeps inactive states running at zero volume, and Epic notes for muted music that without this setting a sound can be destroyed and not return.

Step 3: add the component

Add the component to an actor. The bundle's README suggests your GameMode. In its StateSounds array, assign one sound per state, in the same order as the EGameMusicState list in Mowjera.h. Assign them yourself: do not rely on automatic loading.

Step 4: play and switch

The example states are Unseen and Alarm. MusicComponent is your reference to the component.

MusicComponent->Play(EGameMusicState::Unseen);

// later, when the player is spotted
MusicComponent->TransitionMode = EMowjeraTransition::Immediate;
MusicComponent->FadeTime = 0.5f;
MusicComponent->SetGameMusicState(EGameMusicState::Alarm);

StopAll() stops the music. Play, SetGameMusicState and StopAll can also be called from Blueprint. Call them after the component's BeginPlay has run.

Which settings can you change?

PropertyWhat it doesStarting value
FadeTimeSeconds a crossfade takes2.0
TransitionModeImmediate, NextBar or NextPhrase (two bars)NextBar

A bar is a short group of beats.

Are your transition times applied automatically?

No. Each arrow's timing, fade and condition text are saved in metadata.json, and the component does not read it. Set TransitionMode and FadeTime before each change. A delayed switch uses the FadeTime that is set at the moment the fade begins. See game music transitions.

How do you control layers?

A Layer stems export adds a second array, LayerSounds. Assign the stem sounds in the exact order listed in the comment at the top of Mowjera.h. Loop them and set virtualization the same way.

MusicComponent->SetIntensity(0.7f);
MusicComponent->SetParameter(FName("danger"), 0.4f);
const float Danger = MusicComponent->GetParameter(FName("danger"));

Values run from 0 to 1, and danger stands for a parameter your score defines. The layered component also has MasterVolume and ParameterRamp (0.35 seconds). SetParameter calls made before BeginPlay are ignored. Calling SetParameter in the middle of a state crossfade applies the shorter ramp time to the rest of that fade.

What you should hear

The first state loops. A state change waits for its timing point, then crossfades.

If something goes wrong

ProblemFix
No soundCheck that StateSounds is filled and that Play runs after BeginPlay
The wrong music plays for a stateReorder the array to match the enum
A state or layer never comes backCheck Looping and Virtualization Mode on that SoundWave
An unknown parameter does nothingThe component ignores it silently. Copy the id from Mowjera.cpp
.ogg files will not importStandard Unreal does not import them. Use the WAV export

What to try next

Plan your values with intensity layers and parameters, then rehearse them with test your adaptive music.

Try it now

Reading helps; clicking teaches. Open your dashboard and do the steps.

Open my dashboard