Search Unity

NPC Design - One Script for One NPC?

Discussion in 'Scripting' started by forthright48, Jun 30, 2015.

  1. forthright48

    forthright48

    Joined:
    Feb 20, 2014
    Posts:
    2
    Hey guys. I am having trouble in designing dynamic dialogue system for NPC. Here is the scenario:

    I have a "Dialogue System", where when I pass a TextAsset, it displays it line by line. So for each NPC, I simply assign a TextAsset, and when player interacts with the NPC ( onTriggerStay and keypress) it sends the TextAsset to "Dialogue System".

    With fixed dialogue with NPC, there is no issue. But what if I want to change the dialogues of certain NPC. Lets say that there is an NPC X. X says "It is sunny today" most of the time, except for the days when it is not sunny. So, I can simply attach another script with the NPC, lets call it DialogueControllerForX and let it handle the transition (gather data from gamecontroller and decide which dialogue will be spoken by it).

    Now, the problem is, if I want 100 NPC to speak dynamically, I will need 100 scripts. Is there any better solution?

    Also, is there any book or tutorial handling these kind of game design/architecture?

    Thanks in advance.
     
    Last edited: Jun 30, 2015
  2. Gigiwoo

    Gigiwoo

    Joined:
    Mar 16, 2011
    Posts:
    2,981
    This belongs in scripting. And, to answer your question - you do NOT need separate scripts for this. You simply need variables, like public string[], which you can then assign in the editor. You can get VERY sophisticated with this, having a variety of dialog responses, that you pick randomly based on a variety of conditions.

    Gigi
     
    Last edited: Jun 30, 2015
    forthright48 likes this.
  3. Korno

    Korno

    Joined:
    Oct 26, 2014
    Posts:
    518
    Second what Gigiwoo said. You need to separate the data, the dialog system and the NPCs that use the data.
    Also, dont assume you need an NPC to have a dialog (I ended up having a talking book, long story...)

    An NPC should have dialog data (the conversation) and when the player starts a conversation with an npc - that interaction would trigger the dialog system - which would pick up the NPCs dialog data and use that.

    Now, you can have:

    very simple dialogs - where the dialog data is just a single string and all the dialog system does is just display that string. It is static simple. Very easy to implement.

    Medium systems: the dialog data may allow user variables in (for example "Hello %Player.Name%" ) which will the be replaced with the correct name number etc.

    More complex systems: Which have the above and also allow changing the dialog based on game state. Like your example where on sunny days he says its sunny but on rainy days he doesnt. This will use some kind of precocessing - xml or something and is much more complex.

    And then you can have player responses, again these can be static (i.e. always have the option to say that) or dynamic (depending on the game state some replies might be possible).

    Finally some conversations will trigger new game actions/states - like if you chose to not help someone, that character dies etc. You need someway to execute code based on a conversation.

    But the main point is, this is your Dialog system. It just executes the dialog based on the dialog data. There is only one system, you dont rewrite it for every NPC. Give your NPC the correct dialog data and let the dialog system drive it.


    Here is how I store dialog data in my game:

    Code (CSharp):
    1. namespace NekoEngine.Data
    2. {
    3.  
    4. public class DialogData : ScriptableObject
    5. {
    6.     [System.Serializable]
    7.     public class DialogReply
    8.     {
    9.         public string Reply;
    10.         public DialogData NextDialog;
    11.     }
    12.    
    13.     [Multiline()]
    14.     public List<string> Message = new List<string>();
    15.    
    16.     public List<DialogReply> Replies = new List<DialogReply>();
    17. }
    18.  
    19. }
    It is a scriptable object (some NPC will share the same dialog - we dont want to dubplicate those dialogs in memory) and has a list of messages that are displayed. When those messages have been finished the player has a list of replies (or none sometimes) each reply can link to another dialogdata to continue the conversation..

    The dialog system itself is quite complicated but if you want to see it just ask.
     
    Gigiwoo and forthright48 like this.
  4. forthright48

    forthright48

    Joined:
    Feb 20, 2014
    Posts:
    2
    Thanks for the replies guys and sorry that I posted in wrong section of the forum. I will post about scripting on this section from now on.

    So basically, instead of assigning a TextAsset, I should assign a scriptable object (Dialogue Data) to NPC? Okey, that makes sense. I will think more on it.
    This sounds important. I will try to focus on this.

    Thanks once again. I will post updates.