Search Unity

Let's have a talk about dialogue tree *implementation*

Discussion in 'Scripting' started by CDMcGwire, Mar 4, 2015.

  1. CDMcGwire

    CDMcGwire

    Joined:
    Aug 30, 2014
    Posts:
    133
    After running a search on the forums, was rather surprised how little I found on threads dedicated to this topic. Unless I'm terrible at searching and one was at the bottom of the Game Design page.

    Anywhoo, I want to talk about how people have gone about with the creating of dialogue systems in Unity specifically. I'm aware of the Asset Store products, and am sure they're great. However, this is something I wish to be able to develop from scratch(ish) for the sake of being a creator.

    I'm a D.I.Y. type of guy, until I F.I.U. and just B.U.Y. it.

    So, is it common to make entirely script based systems? How have you componentized them? How do you treat it in the realm of game objects? Creating a unique response for every player choice is obviously a great way to go insane, so how much Uniqueness(tm) in dialogue paths do you find manageable?

    I ran a test

    [Which can be found here ->http://www.kongregate.com/games/GoodLifeMyDeluxe/simple-web-text-adventure]

    And in it, I used a series of game objects with components that held the necessary text info and "pointers" to the game object component of the next prompt. Then a manger object handles the processing of button events and goes down the tree. Now, for the sake of quick prototyping, this is an exponentially increasing complexity type of tree with four set responses (which was fun as a creative writing warm-up). There are 64 different "endings."

    My reasoning behind making my dialogue "nodes" into game object components was to use the editor's built in features to make an interface for hastily adding, hooking up, and writing the contents of each node. I used the Hierarchy window to visualize the dialogue paths, and public component variables to drag and drop the prompts to their connection points.

    However, while it worked, it wasn't ideal. So, for sake of discussion, learning, and continuing to grow my connections with this community, I decided to discuss it here.

    (Please, God, let this have been the right section of the forums for this topic lest I invoke the wraths of the Mods.)
     
    Nanako likes this.
  2. AndrewGrayGames

    AndrewGrayGames

    Joined:
    Nov 19, 2009
    Posts:
    3,821
    Hate to tell you, but...*



    *: It's not really a "wrath" thing so much as, "...Moving it." No worries bro. :)
     
    Last edited: Mar 4, 2015
    chrisall76, CDMcGwire and hippocoder like this.
  3. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    I'm sorry but Wrath of the Asvarduil has been invoked so I'll have to move this.
     
    CDMcGwire and AndrewGrayGames like this.
  4. CDMcGwire

    CDMcGwire

    Joined:
    Aug 30, 2014
    Posts:
    133
    Okay! Wasn't sure if it was scripting, as I was thinking of a high level discussion of practice and didn't want people to start posting code. But I'm still getting used to the rules of this forum, so thank you!
     
  5. AndrewGrayGames

    AndrewGrayGames

    Joined:
    Nov 19, 2009
    Posts:
    3,821
    Moving back on topic...

    I've actually implemented a sort of proto-dialogue tree system in Sara the Shieldmage.

    What I do, is I created a serializable EntityText class. This class has the name of the speaker, the dialogue text, 1 to n dialogue choices (each one is pretty much button text, and a node ID to something else in the same TextThread), and 0 to n DialogueEvents (a string/string pair that talks to various components on the Dialogue Presenter's controller to do special things like give items, or start battles against a pre-determined enemy formation, or whatever.)

    I don't think you have to create a game object like you've noted, but I think the idea to pass some way of pointing to a different text node is a great way to do things.
     
  6. CDMcGwire

    CDMcGwire

    Joined:
    Aug 30, 2014
    Posts:
    133
    So where do you do your writing, the Unity editor or your choice of IDE? And where do components come into your implementation?

    For the purposes of quickly adding and editing dialogue as necessary, I'm considering learning how to extend the editor. It's good practice for my #elitecodingskillz (tm) as well, so it's something to look into.
     
  7. AndrewGrayGames

    AndrewGrayGames

    Joined:
    Nov 19, 2009
    Posts:
    3,821
    I started off using MonoDevelop (comes with Unity), but I went ahead and downloaded a free edition of Visual Studio 2013 about two or three months ago, and it's really paid off! VS is just a lot less quirky than MonoDevelop, when working with C#.

    Another thing I do, is I save my dialogue trees as JSON files (this is such an example)*, and have a JSON loader behavior that loads the file for a specific NPC. I do that, because I don't want to load the entire game's dialogue in a single scene, or even ahead of time on initialization - it's just wasteful! Instead, I load what I need, which keeps my memory footprint low(ish). I recommend SimpleJSON, even though the developer of that abused the hell out of properties (in C#, a property is nothing but a getter and/or setter method that can't take any additional arguments. In other words, pure, sweet syntactic sugar. Too much of it will give you diabetes just the same, too. The reason it's abusive is a field - a public variable - has a lower footprint, since it's not generating two method handles behind the scenes, and using them for everything wastes memory that a bunch of public fields wouldn't. Of course, properties let you perform other in-situ operations, like formatting, and just using a getter can prevent other code from mucking with a field, if you don't want other stuff messing with it. It's just another tool in your belt.)

    *: You'll notice the extension is *.txt, when it should be *.json. That's because Unity won't recognize it as a local text asset unless the extension is *.txt. It's non-standard practice, but as long as Unity copes with it, all's good.
     
    Last edited: Mar 5, 2015
  8. CDMcGwire

    CDMcGwire

    Joined:
    Aug 30, 2014
    Posts:
    133
    Oh, when I wrote that first question I was asking where you input the actual dialogue text. Though I do agree with using visual studio. That's what I've been doing. :p

    I like that idea of saving the dialogue in a file. That would be great for managing large amounts of dialogue or reusing it. I'm thinking I need to find a way to write a tree in the editor, generate a file from there, then attach the file to a dialogue component on an NPC (or anything really.)
     
  9. knr_

    knr_

    Joined:
    Nov 17, 2012
    Posts:
    258
    Anybody try creating a custom editor window that worked like a flowchart (like the Animator window)? Might be good for this. I Google'd it and didn't come up with anything unfortunately.
     
  10. fox4snce

    fox4snce

    Joined:
    Jan 25, 2014
    Posts:
    74
    I don't have the slightest clue how to do custom editor windows in unity.

    When I wrote my very simple dialogue system, I just used an XML file.

    <npc_says> blah blah blah </npc_says
    <player_response> blah blah </player_response>
    <link>
    <player_response> blah blah </player_response>
    <link>
    <player_response> blah blah </player_response>
    <link>

    <npc_says> blah blah blah </npc_says> etc...


    The link just points to the next dialogue bit.

    All I had to do is load it into a list.

    I had dreams of extending it so that it would also call functions/actions but I never got that far.
    I also wanted to do a simple GUI editor to make it super silly simple for anyone on my team to write their own dialogue.
     
  11. Sharp-Development

    Sharp-Development

    Joined:
    Nov 14, 2013
    Posts:
    353
    Just as a sitenote, Unity5 now makes State machines available for quiet everything via the mecanim node editor. It may be worth looking into it since it effectively ships a tree/node setup with proper dialog states for free. Tho, since this is just a suggestion I dont quiet know how much is acctually possible via this method. ;)
     
    AndrewGrayGames and CDMcGwire like this.
  12. CDMcGwire

    CDMcGwire

    Joined:
    Aug 30, 2014
    Posts:
    133
    Hmm... I'll have to check that out after work. Thanks for the tip!
     
  13. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,697
    I have some experience with dialogue trees. Like the other folks here, I also recommend JSON, XML, or some other format that's not tied to GameObjects. It's more resilient to script changes, and you can process it externally to do things like run it through a spell checker or hand it off to a translator. If you ever switch game engines, it'll still be in a usable format. You can always write a GUI front-end in Unity or an editor extension to import and export it from a Unity 5 state machine.
     
    AndrewGrayGames and CDMcGwire like this.
  14. CDMcGwire

    CDMcGwire

    Joined:
    Aug 30, 2014
    Posts:
    133
    That's pretty big right there. I don't even need unity to work on or process that kind of thing.
     
    AndrewGrayGames likes this.
  15. AndrewGrayGames

    AndrewGrayGames

    Joined:
    Nov 19, 2009
    Posts:
    3,821
    It's always pleasant when I mess with a dialogue/item/enemy blob, save it, and go back to Unity waiting for the compile step to finish...and then remember, oh yeah, it doesn't compile! *Hits play button*
     
  16. baj00

    baj00

    Joined:
    Nov 1, 2010
    Posts:
    25
  17. AndrewGrayGames

    AndrewGrayGames

    Joined:
    Nov 19, 2009
    Posts:
    3,821
    This is software development. There is no such thing as a truly definitive solution. One project may work just fine with an out-of-the-box Asset Store solution. Another may require a fully-custom system.
     
    CDMcGwire and TonyLi like this.
  18. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,697
    Cases in point:
    • Some Dialogue System users read text blobs from external sources like MySQL databases at runtime, including procedurally-generated or user-generated conversations.
    • A few studios port data from Neverwinter Nights 1 & 2 and Dragon Age into Unity.
    • Many the larger studios use articy:draft to manage their content pipeline, including dialogue, so they need a way to get this running in Unity.
    • A lot of studios export to Excel to outsource translation.
    • Some projects heavily rely on cutscene sequences attached to each dialogue line, while others only use the line data and script-based conditions, such as for AI knowledge base projects.
    The only reason why the Dialogue System might approach "definitive" is because it has a huge editor-time code base to handle all these cases, and it took quite a while to develop. (The runtime code base is pretty lean, though.) Even so, it certainly doesn't do everything because developers are always coming up with new feature requests.

    Like @Asvarduil writes, if you're writing your own system, just make it do what's necessary to get your project done. Otherwise you could spend forever on the tool and never actually write your game.

    Unity 5's new state machines seem like a great way to get a solid, powerful user interface for free. Just write a "dialogue line" behavior that you can attach to each node. It could have a field for the dialogue text and perhaps other fields such as who's speaking, etc. You can even use the state machine's built-in transition conditions in your conversation tree.
     
    CDMcGwire likes this.