Calling custom events is a convienent way of integrating new code or third-party assets into Adventure Creator, as they can be called in addition to AC's regular code - rather than instead of. Adventure Creator's EventManager script provides a number of event handlers, which are listed in the scripting guide.
For the purpose of this tutorial, we'll add an event listener that gets called whenever a character speaks - this could be useful if we were looking to integrate a custom lip-syncing solution, for example. As this tutorial covers scripting, a basic knowledge of C# is assumed.
The EventManager script has a static event named OnStartSpeech, which is called whenever a character begins speaking. According to the scripting guide, any listener requires the following parameters:
- AC.Char speakingCharacter - The character who is speaking. If null, the line is considered to be a narration
- string speechText - The dialogue text
- int lineID - The ID number of the speech line, as generated by the SpeechManager
Create a new C# script named SpeechListener and add the AC namespace at the top:
using UnityEngine;
using System.Collections;
using AC;
public class SpeechListener : MonoBehaviour
{
}
Let's create a function that acts as our listener. Name it GetSpeech, and add the parameters listed above:
void GetSpeech (AC.Char speakingCharacter, string speechText, int lineID)
{
}
Inside the function, add a simple Debug.Log to output the parameter values so that we can confirm it works:
void GetSpeech (AC.Char speakingCharacter, string speechText, int lineID)
{
Debug.Log ("Character: " + speakingCharacter + ", Text: " + speechText + ", lineID: " + lineID);
}
We now just need to add this function as a listener to the OnStartSpeech function. If we do this within an OnEnable function, it will be added when the scene begins:
void OnEnable ()
{
EventManager.OnStartSpeech += GetSpeech;
}
To prevent memory leaks, we should also remove this similarly in an OnDisable function:
void OnDisable ()
{
EventManager.OnStartSpeech -= GetSpeech;
}
And that's it! If you add this script to your Adventure Creator scene, you should find that the speech text is output to the Console as it is triggered: