Lesson 8

Table of Contents

Routing Callback Notifications

[Note]

Before engaging this section, you must complete the preceding section: Exposing Callback Flags.

Currently, the CallbackFunction() function is only being called when the MusicSyncBar Callback Flag is received from the sound engine, in turn making our trees move to the music's tempo. As you need to also add the MusicSyncUserCue notification, you will need to set up some conditional logic like an If Statement to differentiate which notification you are receiving in the CallbackFunction() function. An If Statement will receive an input, then evaluate it against a given condition, so that the programmer can execute a piece of code when the condition evaluates to 'true'. Otherwise, when the If statement returns 'false', it will skip that segment of code.

To write an if statement, you simply write 'if' followed by a set of parentheses '()' which will include the boolean expression to evaluate, and curly braces '{}' including the code to run. Let's take a very basic example.

if(number == 42) { 
    MusicEvent.Post(gameobject); 
}

In this example, the MusicEvent would only be posted if the number variable would be set to 42 but ignored if the number was not 42. As such, when the value of the number variable is 42, meaning the condition inside the If Statement would be true, the MusicEvent will be posted. Likewise, you can use an If Statement to evaluate whether you are receiving a Music Custom Cue or Music Segment Bar notification.

  1. With the Wwise game object selected, open the PostWwiseEvent script again by double-clicking.

  2. Make a new empty first line in the CallbackFunction, following the opening curly brace.

    void CallbackFunction(object in_cookie, AkCallbackType in_type, object in_info){
        GameManager.PushRhythmAction();
    }

  3. Declare an If Statement by typing if, followed by a pair of brackets (), and lastly a pair of curly braces {}.

    void CallbackFunction(object in_cookie, AkCallbackType in_type, object in_info){    
        if(){}
        GameManager.PushRhythmAction();
    }

    Let's now declare the boolean expression. What you need to evaluate is whether the AkCallbackType in_type property is a Custom Cue, and if so allow it to post the enemy theme. To compare two variables or properties, you can use the double equal sign (==) operator.

    [Tip]

    If you want to make a comparison where you want something not to be equal to the following variable, you can use the (!=) operator.

  4. In the If Statement parentheses type in_type followed by ==.

    if(in_type == ){} 
     

    You can now compare it with the type of property you want it to be. You already know how to declare a AkCallbackType from Lesson 7, but this time we will specify the type to be AK_MusicSyncUserCue.

  5. Continued after the == operator, type AkCallbackType.AK_MusicSyncUserCue.

    if(in_type == AkCallbackType.AK_MusicSyncUserCue){} 
     

    You are almost there! All you need now is to post the Music_Enemies Event from inside the if statement and you will be done. As such, you need to create a new Wwise-Type Event class, assign the Music_Enemies Event and post it within that If Statement.

  6. Make an empty line below the MusicEvent property and type public AK.Wwise.Event MusicEvent_Enemy;

    public class PostWwiseEvent : MonoBehaviour {
        public AK.Wwise.Event MusicEvent;
        public AK.Wwise.Event MusicEvent_Enemy;
        public AK.Wwise.CallbackFlags CallbackTypes;

    You've created your Event property, MusicEvent_Enemy, so it can now be referenced inside your If statement.

  7. Inside the If Statement's curly braces {} type MusicEvent_Enemy.Post(gameObject);, and save the script.

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
     public class PostWwiseEvent : MonoBehaviour {
        public AK.Wwise.Event MusicEvent;
        public AK.Wwise.Event MusicEvent_Enemy;
        public AK.Wwise.CallbackFlags CallbackTypes;
        // Use this for initialization.
        void Start () {
                    MusicEvent.Post(gameObject, CallbackTypes, CallbackFunction);
        }
        void CallbackFunction(object in_cookie, AkCallbackType in_type, object in_info){
            if(in_type == AkCallbackType.AK_MusicSyncUserCue){
                MusicEvent_Enemy.Post(gameObject);
            }
            GameManager.PushRhythmAction();
        }
    }

    Lastly, you need to select the Music_Enemies Event in the Inspector.

  8. In the Unity Inspector's PostWwiseEvent script, open the MusicEvent_Enemy property, go to Events > Music > General and select Music_Enemies.

    While keeping the MusicSyncBar callback notification, you should also add the MusicSyncUserCue callback notification to the CallbackTypes property.

  9. In the Unity Inspector's PostWwiseEvent script, click the CallbackTypes property and select MusicSyncUserCue.

    You can now test the implementation.

  10. Connect the Wwise profiler to the game, and enter Play mode in Unity, as shown in Lesson 1.

  11. When the game is loaded, go to the WAG menu and enable GOD MODE.

    Since the Music_Region Event is triggering the Music_Enemies Event by a Custom Cue at the end of its Music Segments, you will have to wait until the Music_Region Event has finished playing one Music Segment before the Music_Enemies Event will start to play.

  12. Engage in a fight with the enemy creatures and pay attention to the enemy music.

    At first, the enemy theme will not be audible, even though it will be active. Then, when getting closer to an enemy, the theme volume will slowly increase according to the distance to the nearest enemy. You may now exit Play mode.

  13. In Wwise, select the Voices tab and ensure that both the Music_Regions and Music_Enemies Events are being played.

Congratulations! You've not only managed to post the Music_Enemies Event, but used an If Statement with a condition operator to specify what type of input the function would be receiving, thereby enabling the same function to do different operations based on its input. This is a very fundamental part of programming.