FMOD Events in Unreal Engine 5 (Play Event 2D) - Game Audio Basics
Si prefieres leer una versión en Español, da click en este botón =>
Unreal Engine Version: 5.0.3
FMOD Version: 2.02.07
This blog post will guide you on how to play an FMOD “2D Event” in Unreal Engine 5 using Blueprints and C++ code. We will create a simple One-shot type event and a looping music type event. Additionally, we will give you a simple algorithmic design to handle the triggering behavior of these events.
We will trigger our actions for these tests by pressing the E, R, and T keys. Finally, I will show you how to bind these keys to our functions in Blueprints and C++.
We will create all the blueprint examples inside the BP_ThirdPersonCharacter class and define the C++ ones in the MyUE5ProjectCharacter.cpp file. For this tutorial, we will learn how to implement these events using both C++ and Blueprints, so make sure to open both classes.
IMPORTANT!
〰️
IMPORTANT! 〰️
Prerequisites:
Create an empty C++ third-person project called “MyUE5Project”.
Download and integrate FMOD’s plugin into your project.
Create a new FMOD Studio project and set your bank’s output directory to MyUEProject/Content/FMOD
Build your Master Bank
For this tutorial to make sense, you need to know how to integrate the FMOD plugin and call its properties in Unreal Engine. Additionally, consider reading and referencing my previous post about UPROPERTY & UFUNCTION:
IMPORTANT!
〰️
IMPORTANT! 〰️
FMOD Setup:
Open your FMOD project and create two 2D Timeline Events, one called “Play_MusicLoop” and another called “Play_OneShot.”
Import two audio files into these events, a short one-shot type (A UI sound effect will work fine for this example) and a looping music or ambiance sound. If you prefer, feel free to download and use these sounds below.
Select both events, right-click them, and assign them to the Master Bank.
Now, go ahead and build the Master Bank.
Go back to your Unreal project. You should be able to see your new events in the Content Drawer inside MyUEProject/Content/FMOD/Events.
C++ Setup:
Lets implement our events inside the main Player Pawn to simplify our testing. You can find the C++ parent class in MyUE5Project/MyUE5ProjectCharacter and the child Blueprint Class in MyUE5Project/Content/ThirdPerson/Blueprints/BP_ThirdPersonCharacter.
Add this directive to access the FMOD API:
.h file:
#include "FMODBlueprintStatics.h"
Declare an FMOD event property and an Event Instance:
.h file:
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category="Audio") UFMODEvent* myEvent; UPROPERTY(EditAnywhere, BlueprintReadWrite, Category="Audio") FFMODEventInstance myEventInstance;
Declare three functions, PlayEvent(), PauseEvent(), and StopEvent():
.h file:
UFUNCTION(BlueprintCallable, Category="Audio") void PlayEvent(); UFUNCTION(BlueprintCallable, Category="Audio") void PauseEvent(); UFUNCTION(BlueprintCallable, Category="Audio") void StopEvent();
.h file:
Save your code and compile it in the editor.
You should be able to see this properties in the blueprint graph of your BP_ThirdPersonCharacter class:
One-Shot Sounds:
These types of sounds don't require much control. They tend to be short in length, and many can exist simultaneously. In most cases, triggering them is enough to achieve the desired behavior. We can define the maximum number of instances and a “cool-down” time in FMOD Studio.
IMPORTANT:
Assign the “Play_OneShot" event to My Event property
Blueprint:
.cpp file:
void MyUE5ProjectCharacter::PlayEvent() { UFMODBlueprintStatics::PlayEvent2D(this, myEvent, true); }
Parameters/Arguments:
WorldContextObject (UObject*) = this
The object in the world where this event is attached, but won't follow its position. Use this as default
Event (UFMODEvent*) = myEvent
The Fmod event to be played
bAutoPlay (bool) = true
Wether it plays the event as soon as the function its called or not
.cpp file:
Looping Sounds:
In most cases, we don't want multiple instances playing simultaneously for looping sounds. Music and ambiances are clear examples of these types of sounds. To achieve this, we need to create a simple system that checks if an instance of an event exists or not. Using an FMOD Event Instance property and some flow control techniques, we will create an algorithm that checks if an Instance exists, then we can Pause/Unpause, Stop and Start the event again. Only one instance of this event will play at a time.
IMPORTANT:
Assign the “Play_MusicLoop" event to My Event property.
Play Event:
Blueprint:
.cpp file:
void MyUE5ProjectCharacter::PlayEvent() { if (UFMODBlueprintStatics::EventInstanceIsValid(myEventInstance)) { UFMODBlueprintStatics::EventInstanceSetPaused(myEventInstance,false); } else { myEventInstance = UFMODBlueprintStatics::PlayEvent2D(this, myEvent, true); } }
Parameters/Arguments:
paused (bool) = false
Wether to pause your event instance or not
WorldContextObject (UObject*) = this
The object in the world where this event is attached, but it won't follow its position. Use this as default
EventInstance (FFMODEventInstance) = myEventInstance
The EventInstance property to manipulate
Event (UFMODEvent*) = myEvent
The Fmod event to be played
bAutoPlay (bool) = true
Wether it plays the event as soon as the function its called or not
Pause Event:
Blueprint:
.cpp file:
void MyUE5ProjectCharacter::PauseEvent() { UFMODBlueprintStatics::EventInstanceSetPaused(myEventInstance,true); }
Parameters/Arguments:
EventInstance (FFMODEventInstance) = myEventInstance
The EventInstance property to manipulate
paused (bool) = true
Wether to pause your event instance or not
Stop Event:
Blueprint:
.cpp file:
void MyUE5ProjectCharacter::StopEvent() { UFMODBlueprintStatics::EventInstanceStop(myEventInstance, true); }
Parameters/Arguments:
EventInstance (FFMODEventInstance) = myEventInstance
The EventInstance property to manipulate
Released (bool) = true
Wether to release the event instance content after it stops
.cpp file:
Complete Blueprint Graph:
Extra - All in C++?:
If you prefer staying entirely in C++, I will show you how to create a direct reference to the FMOD AudioEvent assets and Bind your keyboard keys to trigger your functions.
Direct reference to an asset:
Go to the Content Drawer and find the event or asset you want to reference, right-click on it and select Copy Reference to get your event’s path.
Open your .h file and paste your event’s path to your FMOD event property inside the TEXT(“ “) argument:
.h file:
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category="Audio") UFMODEvent* myEvent = LoadObject<UFMODEvent>(nullptr, TEXT("FMODEvent'/Game/FMOD/Events/Play_OneShot.Play_OneShot'"));
Key Bindings to Your Functions:
Inside the .cpp file, find the SetupPlayerInputController() function and add your bindings inside. These lines of code achieve the same as calling “Keyboard Events” in Blueprints by binding the keys E, R, and T to your PlayEvent(), PauseEvent(), and StopEvent() functions.
.cpp file:
void AMyUE5ProjectCharacter::SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) { PlayerInputComponent->BindKey("E", IE_Pressed, this, &AMyUE5ProjectCharacter::PlayEvent); PlayerInputComponent->BindKey("R", IE_Pressed, this, &AMyUE5ProjectCharacter::PauseEvent); PlayerInputComponent->BindKey("T", IE_Pressed, this, &AMyUE5ProjectCharacter::StopEvent); }