Page 1 of 1

How to return to previous conversant node after ending the conversation?

Posted: Tue Apr 30, 2024 10:54 pm
by Cobined
I'm making a game where there are multiple NPC characters, and each one has their own dialogue trees. But I'm having trouble figuring out how the player can return to the last node the NPC spoke before the player decided to end the conversation.


More clarification/example:

NPC: "Hey!"
Player: a)"Hello!" b)"You look awesome!"
NPC: a)"What brings you here?" b)"Thanks for the compliment."
Player: a.1)"Pizza brings me here." a.2)"I'll talk to you later."(<- Ends conversation, and when the player probably goes off to talk to someone else. When they come back to this NPC, I want it to resume when the NPC asks "What brings you here?") b.1)"No problem! By the way, what's behind you?" b.2)"I'll talk to you later."(<- Ends conversation, and when the player comes back to this NPC, I want it to resume when the NPC asks "Thanks for the compliment.")

Re: How to return to previous conversant node after ending the conversation?

Posted: Tue Apr 30, 2024 11:22 pm
by Tony Li
Hi,

Here are two ways you can do it:

Approach 1: Set a variable true when the conversation gets to "What brings you here?". Add a link from <START> to "What brings you here?" with a condition that the variable is true. This approach doesn't require scripting, but it does require more manual labor. However, it gives you the most control over where each conversation resumes, which can result in more natural-feeling dialogue since you customize each conversation.

Approach 2: This requires scripting, but it automates things. Add a script with an OnConversationLine(Subtitle) method to the Dialogue Manager. In this method, if the line is spoken by the NPC record the entry ID:

Code: Select all

Dictionary<int, int> lastNPCEntryID = new Dictionary<int, int>; // key is NPC Actor ID, value is last entry ID

void OnConversationLine(Subtitle subtitle)
{
    if (subtitle.speakerInfo.IsNPC)
    {
        lastNPCEntryID[subtitle.speakerInfo.id] = subtitle.dialogueEntry.id;
    }
}
Also add an OnConversationStart(Transform) method that checks if there's a recorded entry ID for the NPC. If so, jump to that entry:

Code: Select all

void OnConversationStart(Transform actor)
{
    var npcID = DialogueManager.masterDatabase.GetConversation(DialogueManager.lastConversationID).ConversantID;
    if (lastNPCEntryID.ContainsKey(npcID))
    {
        var entry = DialogueManager.masterDatabase.GetDialogueEntry(DialogueManager.lastConversationID, lastNPCEntryID[npcID]);
        DialogueManager.conversationController.GotoState(DialogueManager.conversationModel.GetState(entry));        
    }
}