Switches are a type of Game Sync that allow us to define variations for a specific sound object. They are used to implement multi-surface footsteps sounds for example, by switching to the correct set of sounds depending on the surface the player is walking on. In contrast to States, Switches are applied on a local game object scope, that means that the Switch is applied on a particular game object and can exist for many instances of individual game objects. In this tutorial we will learn how to set Switches by using Blueprint nodes and by writing C++ code in Unreal Engine 4.
Setting a Wwise Switch by using Blueprint nodes
We can use the Set Switch node to set a Wwise Switch. Remember that Switches work on a local Game Object scope, so you need to specify on which actor you want to call the Switch node. In this example we are setting a Switch and posting an Event when starting the game:
If you have an Ak Component in your actor, you can use the Set Switch node and connect the Ak Component reference to the Target field:
Setting Wwise Switches with C++
We can use the SetSwitch
method of the AkSoundEngine namespace to directly set a Switch in code. The code looks like this:
UAkGameplayStatics::SetSwitch(FName SwitchGroup, FName SwitchState, AActor *Actor);
SwitchGroup is the Switch Group created in the Wwise authoring tool. SwitchState the Switch we want to set. Actor refers to the Actor in which we post the Event playing the Switch Container.
UAkGameplayStatics::SetSwitch calls the SetSwitch function of the underlying audio device singleton. We could also directly call it to set the Switch:
FAkAudioDevice* AudioDevice = FAkAudioDevice::Get();
if (AudioDevice)
{
AudioDevice->SetSwitch(*FName("SwitchGroup").ToString(), *FName("SwitchName").ToString(), this);
}
We can also set a Switch on an UAkComponent
:
AkComponent->SetSwitch(FString SwitchGroup, FString SwitchState);
The parameters here are the same as before: SwitchGroup is the Switch Group created in the Wwise authoring tool. SwitchState the Switch we want to set. Note that we don’t need to pass an Actor as an argument if we are using a UAkComponent. That’s it!