In this tutorial we’ll take a quick look at how to post Wwise Events in Unreal Engine 4 by using the Blueprint nodes provided by the integration plugin and by writing C++ code.
Wwise Events Setup in Unreal Engine 4
To post Wwise Events in Unreal Engine 4 we need to take a few preparation steps fist, namely:
- Creating Wwise Event assets.
- Creating Wwise SoundBank assets.
- Associating Events to the corresponding Soundbanks.
- Generating the SoundBanks inside of Unreal Engine 4.
After completing these steps, we can finally play our sounds in the game. We can manually create the assets in content browser or use the provided Wwise Picker to speed up the first step. Let’s take a look at how it works.
Manually creating Events and Soundbanks assets in Unreal Engine 4
Right click on the content browser and select Audiokinetic Event to create a new Wwise Event:
Name the Event after an Event you created in the Wwise Authoring app. Create an Audiokinetic Bank asset in the same way:
Make sure to name the SoundBank to one that contains the Event created earlier. Double click on the Event and select the corresponding bank in the Required Bank field:
As a last step go to the Build drop down settings and select Generate SoundBanks…:
In the Available Banks field, select the Soundbank asset created in the last step and click on Generate:
Create Wwise Events assets using the Wwise Picker
Instead of manually creating Event assets in the Content Browser, you can simply open the Wwise Picker under Window->Wwise Picker and drag & drop the desired Event into the Content Browser:
You will still have to create the Bank assets and assign the Events to the corresponding SoundBank. This method still saves some clicks.
Posting Wwise Events by using the AkAmbientSound actor
The AkAmbientSound is an Actor that holds an Ak Component component. Locate the Ambient Sound from the Modes tab on the left and drag it into the level:
We can choose our desired Wwise Event in the Details tab of the actor and select if we want to automatically post the when the game starts by checking the Auto Post checkbox:
Posting Wwise Events by using Blueprints
The Wwise Integration plugin provides us with different easy-to-use nodes that we can use to play and manage Wwise Events. Let’s take a look at the various nodes.
Posting Wwise Events using the Start Ambient Sound node
If we don’t want to auto post an Event by deselecting the Auto Post setting in the Ak Ambient Sound, we can start the sound ourselves by using the Start Ambient Sound node:
In a similar way we can stop the Ambient Sound using the Stop Ambient Sound node:
If you look closely at the details tab of the actor, you will notice that Ak Ambient Sound just contains an AkComponent:
Instead of using the nodes displayed above, we could also have used the Post Ak Event and Post Associated Ak Event nodes and connect the AkComponent reference to the target value.
Posting Wwise Events with AkComponent
First, create an AkComponent by selecting an Actor and clicking on Add Component:
In the details tab of the component, select your desired Ak Audio Event:
By using the Post Associated Ak Event node, we will post the Event selected above:
If we want to post an Event that differs from that one selected in the details tab of the component, use the Post Ak Event node:
To stop the Event, use the Stop node and also connect the Ak Component reference to the target pin of the Stop node:
Posting Wwise Events with the Post Event node
The Post Event node is a general node that will post a Wwise Event and attach it to the root component of the specified Actor:
Here we are posting an Event and stopping it with the Execute Action on Playing ID node by using the return value of the Post Event node.
Posting Wwise Events with the Post Event at Location Node
The Post Event at Location node creates temporary Wwise Game Object and posts a Wwise Event at a specified location. In this example we are getting the location and rotation of a mesh component and passing it to the Post Event at Location node:
Posting Wwise Events by writing C++ code in Unreal Engine 4
Posting Wwise Events by writing C++ code is also very easy. We can interface with the same Blueprint nodes provided by the integration plugin or directly gain access to the Wwise SoundEngine.
Preparation for playing Wwise Events with C++
I will use the ThirdPersonExample project that can be created when starting Unreal Engine for all C++ examples. We will directly work inside Character.h and Character.cpp to make things very easy.
In Unreal’s project preferences, switch to the Input settings and create two new Action Mappings:
We will post an Event when pressing the 1 key on our keyboard and stop the sound by pressing the 2 key.
In the ThirdPersonCharacter header file, include AkGameplayStatics.h
and declare UAkAudioEvent
with the UPROPERTY
macro. Also declare a int32 variable named PlayingId:
UPROPERTY(EditAnywhere, Category = "Wwise")
class UAkAudioEvent* Event;
int32 PlayingId;
The Property Specifier EditAnywhere allows us to see and select the Event in the Details tab of our ThirdPersonCharacter:
In the same header file, declare the functions PlayEvent
and StopEvent
:
void PlaySound();
void StopSound();
Before proceeding, let’s also include AkGameplayStatics.h
and bind the two Action Mappings in the SetupPlayerInputComponent
function of our Character.cpp file:
// Wwise
PlayerInputComponent->BindAction("PlayEvent", IE_Pressed, this, &AWwiseTPTutorialCharacter::PlayEvent);
PlayerInputComponent->BindAction("StopEvent", IE_Pressed, this, &AWwiseTPTutorialCharacter::StopEvent);
Posting and stopping Wwise Events using C++
We can now finally implement the two functions we declared earlier. In the Character.cpp file we will post a Wwise Event by calling UAkGameplayStatics::PostEvent
. PostEvent will return an int32 that is the Playing ID of the Event. We will assign the return value of PostEvent to PlayingId:
void AWwiseTPTutorialCharacter::PlayEvent()
{
FOnAkPostEventCallback nullCallback;
PlayingId = UAkGameplayStatics::PostEvent(Event, this, int32(0), nullCallback);
}
To stop the Event we call UAkGameplayStatics::ExecuteActionOnPlayingID
:
void AWwiseTPTutorialCharacter::StopEvent()
{
UAkGameplayStatics::ExecuteActionOnPlayingID(AkActionOnEventType::Stop, PlayingId, 500);
}
Alternatively, we can directly interface with Wwise’s sound engine to post and stop an Event:
void AWwiseTPTutorialCharacter::PlayEvent()
{
PlayingId_2 = FAkAudioDevice::Get()->PostEvent(Event, this);
}
void AWwiseTPTutorialCharacter::StopEvent()
{
FAkAudioDevice::Get()->StopPlayingID(PlayingId_2, 500);
}
The PostEvent
and StopPlayingID
functions of FAkAudioDevice expect a AkPlayingID type, so make sure it to declare it in your header file.
Compile, select your desired Wwise Event in the Character’s Details tab, start the level and press 1 to post and 2 to stop the Event.