Fluent Dialogue

Overview

This is a dialogue conversation framework for unity. Conversations are defined using a fluent api in code.

Features

  • Speech bubbles
  • Conversation trees
  • Multilingual
  • User definable UI
  • Define variables and logic directly in C#
  • Currently only supports the new Unity UI

Note that MonoDevelop has some text formatting issues. I recommend the free Visual Studio Community.

Examples

W/A/S/D to move E to initiate conversation

Example Code

Hello World!

Code
To create a conversation we inherit from Conversation, which is a Monobehaviour, and override GetFirstOption.

Option() creates an option. One of it’s possible parameters is a response. Responses are executed when an option is selected. The top level option’s responses will always be executed when a conversation is started. In this case we simply Yell “Hello World”.

Calling End() on an option sets the conversation to end when the option is selected.

Setup

1. Scene

Add an empty GameObject to you NPC, here mine is called Talk.
TalkObjectOrganization

We then need to add some components to Talk.

Setup0

 

2. Conversation Component

Add your new Conversation component, here called Conversation0 to Talk.

3. TalkAction Component

Add a TalkAction component to Talk

4. Yell Response Handler

For responses to work we need to add their ResponseHandlers. Here we are only using the Yell response so add the YellResponseHandler component to Talk. On the YellResponseHandler we need to specify the GameObject it uses. That GameObject will be SetActive() when yell is called. The first Text Component found will then have it’s text changed to “Hello World”. Yell() takes an optional parameter that specifies the time it should be shown. Notice in this example the Yell Canvas is a world space canvas and a child of Talk.

5. Action Manager

Create an empty GameObject in your scene, mine is called Managers. Now add an ActionManager component to it.

actionmanager

The ActionManager keeps track of all the possible actions the player can execute at a specific time. The use of ActionManager is optional.

6. Initiating the conversation

To initiate a conversation we need to call Execute() on TalkAction. In this example adding the Conversation3DProximityInitiator component will do that for you. When something triggers a collider, in this case a sphere collider, the proximity initiator will tell the ActionManager that a talk action became available for the user to execute. Code defined in Player can then execute ActionManager.Instance.ExecuteClosestAction when a button is pressed. You will likely want to change how the proximity initiator works.

OnConversationStart() and OnConversationEnd()

Code Override OnConversationStart and OnConversationEnd to specify what should happen before and after a conversation starts and ends.

Response Chains

We can chain responses. In this case we yell 3 times. A response needs to complete before the next one in the chain is executed.

Anyone can Yell

Code Yell takes an optional parameter to specify the object doing the yelling. Setup We also need to add a YellResponseHandler component to the specified object.

Options Presenter

Code In this example we have a new method that we can call on an Option. Branch() takes a list of options. These options will be presented by an OptionsPresenter. Option() in this case takes the option’s text as the first argument and the responses when it’s selected as the second argument. Setup A UnityOptionsPresenter component needs to be added to your Talk object to show options. basic_optionspresenter On it you specify the unity UI element that will be SetActive when the Show() or Hide() responses are executed. Options Element is used to specify the UI element that will be filled with your Option Item prefabs. Also specify the prefab that will be used for your Option Items. The Option Item prefab needs to have a Text component, which will be set to the specified option text. You can also create your own options presenters to present your options in whatever way you want.

Dialog Responses

Code Here we introduce a new response. The Text response will write text to a specified UI element. Setup We need to add a TextResponseHandler to the talking object. textresponse_setup On it we specify the text type delay as well as the UI element that contains the unity Text component that will have it’s text changed.

Branching

Code This example shows that any Option can have a branch. To have an option cause the dialog to go back up the tree simply call .Back() on the option

Running Code

Code Here we introduce a new response. Do() takes a delegate which is executed.

VisibleIf

Code We can specify if an option should be shown or not. VisibleIf() takes a delegate that should return a bool. Returning false will exclude that option from the list of options being shown.

Click Initiator

Setup There is a click initiator component included in the example code. You will very likely want to change it to fit your needs.

Parallelized Chains

Code Here we introduce a new Response. Sound() will play an audio resource. There exist cases where we want multiple responses to happen at the same time. Playing sounds while text is being written is a good example. The Then response takes a list of responses, all of them will be started at the same time. Only when all the responses within a Then completes will the next response in the chain be executed.

Only Once

Code Here we introduce a new response. There exists cases where, to control the flow of the conversation, you only want a response to happen once for the current conversation. Imagine you are talking to an NPC, we have sound and text that is played as part of a response, we then go further down into the conversation tree only to return to this option again that plays both the sound and the text. We don’t necessarily want the sound to play again, we only want the text to show. We can then wrap the Sound() response in a Once() response and it will only play once. Its very important to note that the Sound() response will be played again when the conversation is ended and restarted.

Conditional Responses

Code Here we introduce a new response. The If response takes a bool delegate as it’s first parameter and a response chain as it’s second. The response chain will be executed if the bool delegate returns true. It is important to note that the visited number string concatenation in this example is evaluated before the Do() response is executed.

Multilingual

ContinueWhen

Code Here we introduce a new response. ContinueWhen polls the passed in delegate and continues down the response chain once the delegate return true.

Custom Conversation

Code This example shows how to add additional information to the options presenter by inheriting from Conversation. Setup The dialog UI used in this example has a GameObject with the name NPCHeadImage as a child. NPCHeadImage has an Image component. customConversation The effect of this is a Conversation component that you can drag onto your talking object that you can use to set the head image in the editor for individual Conversations conversation_with_headsprite

  • Abdullah Faisal Hamed

    There is an issue with unity 5.1 and the issue tracker refuses to take my issue

    this is the error I have been getting

    MissingFieldException: Field \\\’UnityEngine.EventSystems.EventTrigger.triggers\\\’ not found.

    Reyn.Interaction.FluentDialogue.YellResponseHandler d__3.MoveNext ()

    UnityEngine.MonoBehaviour:StartCoroutine(String)

    Reyn.Interaction.FluentDialogue.YellResponseHandler:HandleResponse(Response, OptionsPresenter)

    Reyn.Interaction.FluentDialogue.TalkAction:HandleConversationResponse(Response)

    Reyn.Interaction.FluentDialogue.TalkAction:SelectDialogOption(Option) (at c:/dev/Unity/dia/DiaDLL/Reyn.Interaction.FluentDialogue/Reyn/Interaction/FluentDialogue/Handlers/TextResponseHandler.cs:69)

    Reyn.Interaction.FluentDialogue.TalkAction:Execute() (at c:/dev/Unity/dia/DiaDLL/Reyn.Interaction.FluentDialogue/Reyn/Interaction/FluentDialogue/Responses/TextResponse.cs:99)

    Reyn.Interaction.ActionManager:ExecuteAction(GameAction) (at c:/dev/Unity/dia/DiaDLL/Reyn.Interaction/ActionManager.cs:126)

    Reyn.Interaction.ActionManager:ExecuteClosestAction(GameObject) (at c:/dev/Unity/dia/DiaDLL/Reyn.Interaction/ActionManager.cs:152)

    Conversation3DProximityInitiator2:OnTriggerEnter(Collider) (at Assets/FluentDialogue/Scripts/Conversation3DProximityInitiator2.cs:34)

    • Mooniepoonie

      Hi.
      I just woke up. I have not yet downloaded 5.1, will do that now and have an answer for you before this time tomorrow.
      I’ll also have a look at the issue tracker.
      Thanks!

    • Mooniepoonie

      Hi.
      Please check the readme for my email address or post yours here so that I can send you the update.
      Thanks