My game has many (hundreds) of dialogs, mostly between the player character and one NPC (though not always). My current way of handling these is to program them into the script that initializes the scene. So, for instance, I’ll set up the character so that when he’s clicked on he has several options, one of which invokes a dialog
if (haventTalkedAboutTown)
John.AddDialogueTopic("Town", "TownIcon", TOWN_DIALOG));
where TOWN_DIALOG is an actual function in my script that gets invoked (via a unityevent)
void TOWN_DIALOG()
{
StartCoroutine(TOWN_DIALOGCo());
haventTalkedAboutTown = false;
UpdateJohn();
}
IEnumerator TOWN_DIALOGCo()
{
Ego.DisablePointer();
Character John = SceneCharacters[0];
Ego.PlayEgoSound("Speech/LBY/A1E5CU36PF1");
while (Ego.isTalking) { yield return new WaitForSeconds(0.1f); }
John.StartDialogue("Speech/LBY/A1E5CU36PF2");
while (John.isTalking) { yield return new WaitForSeconds(0.1f); }
if (John.DialogueTopicsLeft) John.UpdateConversation();
else Ego.EnablePointer();
}
While this solution works, the result is a lot of duplicate code, and if I ever decide to update the implementation, it’s kind of a nightmare. The above example is 11 lines, two functions, for two lines of dialog (the coroutine apparently can’t be invoked by the unityevent.AddListener, which is why there are two functions).
One thing I’ve contemplated is making a small text file in the resources, and loading it through that. So, for instance, the above interaction could be
StartConversation(John, Ego)
SetFalse(haventTalkedAboutTown)
SayDialog(John, Ego, Speech/LBY/A1E5CU36PF1)
Wait
SayDialog(Ego, John Speech/LBY/A1E5CU36PF2)
Wait
UpdateConversation
Then I could have one function that handles the conversation, and parses the file. However, this requires writing a parser that is able to evaluate truth values in the game variables, and be able to understand a lot of different commands. Does anybody have any suggestions about how best to implement a dialog system like this? Am I on the right track?