Search Unity

What would be the best method to create a simple dialogue system?

Discussion in 'Scripting' started by TheDuples, Apr 24, 2019.

  1. TheDuples

    TheDuples

    Joined:
    Nov 13, 2018
    Posts:
    39
    As of right now, for my project I have created a very simple dialogue system that involves a DialogueInteraction Script, in which if the player presses the interact key ("E") while in the trigger area of an NPC, another script which contains the first set of dialogue is activated and presented. I have created the option to have two sets of dialogue where if the first set of dialogue is exhausted, a second set of dialogue is presented, through the deactivation of the first dialogue script. (Think along the lines of how Dark Souls uses its dialogue). Each dialogue script can be any length as dictated in the inspector through an array which can be customized.

    This isn't mean't to be anything fancy, however, each set of dialogue necessarily requires its own dialogue script to be presented only if the other dialogue scripts attached to the NPC are deactivated as a result of being exhausted. If, for whatever reason, I wanted a tertiary or even a quaternary set of dialogue, It would require me to hardcode the activation of said dialogue into the DialogueInteraction script. Overall, this doesn't seem to be terribly efficient and I am wondering what better options are available to me.

    I'm also rather stuck on how I would go about creating deterministic dialogue instances. For instance, if my character were to solve a puzzle, I would require the instigator of the puzzle to show a new dialogue to acknowledge what the player has done, and perhaps have the NPC perform an action upon completion of the puzzle. In addition, I need a system in which if the player solves certain puzzles, that they might be given two or less dialogue choices which would appear only if they had accomplished a certain task.

    Overall, I'm not a fan of unity assets to solve this problem as it doesn't teach me anything, so please don't suggest that I simply buy a dialogue system. I'm mostly looking for resources to solve this problem as best as I can.
     
  2. ProtagonistKun

    ProtagonistKun

    Joined:
    Nov 26, 2015
    Posts:
    352
    You could store the data per npc in a file and use some keywords to access the required dialogue. I.e. this would mean that a player talks to an npc on setting "Chitchat", this loads the data needed and you can output it to the player. After completing a puzzle you can set that npc's keyword to "PuzzleDone" and it will say something related to the puzzle.

    to make this happen I would suggest using an XML file to store the data for a single NPC. From there you can either load a keywords dialogue and output it, or load it all into a dictionary. but in the end it all depends on how big your dialogues are.

    Code (CSharp):
    1. Dictionary<string, ConversationData> npcData;
    If you need a function to be executed on top of this, you could complicate the data structure but I dont know how experienced you are. What I would try to do is have a structure like

    Code (CSharp):
    1. Dictionary<string, KeyValuePair<ConversationData, FUNCTION>> npcData;
    For the function you want to pass, this could be done a few ways, you can make every npc a scriptableobject/prefab and add a UnityEvent to invoke to it, or you could pass a lambda/function directly with a specific type. You can return any value as long as you are consistent about it. A function as parameter works, but you ll need to write quite some logic to run this system.