[HOWTO] How To: Get Conversation Start/End Notifications On Non-Primary Characters

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
User avatar
Tony Li
Posts: 22871
Joined: Thu Jul 18, 2013 1:27 pm

[HOWTO] How To: Get Conversation Start/End Notifications On Non-Primary Characters

Post by Tony Li »

The Dialogue Manager GameObjects and the conversation's primary actor and conversant receive conversation messages such as OnConversationStart and OnConversationEnd. (See Script Messages.) For more info, see Character GameObject Assignments.

If you want notification on another participant at, for example, the end of the conversation, you can use OnConversationEnd on the Dialogue Manager GameObject. It always receives all OnConversationStart/End messages. Or hook into the C# event DialogueManager.instance.conversationEnded. Example: (works best if characters' GameObjects have DialogueActor components)

Code: Select all

void OnConversationEnd(Transform actor) // Put this in a script on Dialogue Manager.
{
    var conversation = DialogueManager.masterDatabase.GetConversation(DialogueManager.lastConversationID);
    var primaryActorIDs = new HashSet<int>() { conversation.ActorID, conversation.ConversantID };
    
    // Identify all non-primary participants:
    var otherActorIDs = new HashSet<int>();
    foreach (var entry in conversation.dialogueEntries)
    {
        var actorID = entry.ActorID;
        if (!primaryActorIDs.Contains(actorID)) otherActorIDs.Add(actorID);
    }
    
    // Send OnConversationEnd messages to them:
    foreach (var actorID in otherActorIDs)
    {
        var actor = DialogueManager.masterDatabase.GetActor(actorID);
        var actorTransform = CharacterInfo.GetRegisteredActorTransform(actor.Name);
        if (actorTransform != null)
        {
            actorTransform.BroadcastMessage(DialogueSystemMessages.OnConversationEnd, DialogueManager.currentActor,  SendMessageOptions.DontRequireReceiver);
        }
    }
}
Post Reply