Controlling the Dialogue System asset using script C#

Announcements, support questions, and discussion for the Dialogue System.
ThierryS
Posts: 5
Joined: Wed Nov 11, 2020 4:28 pm

Controlling the Dialogue System asset using script C#

Post by ThierryS »

I need some help using the Dialogue System for Unity because in my case I cannot manually fill in the field into the Dialog Manager, I need to do this process dynamically (automatically) using C# script.

Is that possible to interact with the Dialog Manager, selecting and setting all the Dialogue Manager options dynamically using via C# script/commands?

Could you please help me to do exactly what is shown in the video “Conversation Conditions” (from the Dialogue System for Unity 2.x video tutorials) just using commands to directly set the parameters/options of the Dialog Manager and thus create the interactive discussion?

Thank you in advance!
User avatar
Tony Li
Posts: 21678
Joined: Thu Jul 18, 2013 1:27 pm

Re: Controlling the Dialogue System asset using script C#

Post by Tony Li »

Hello,

It's possible to do everything using C#.

What is the context of your requirement? This will help me provide the correct information.

The Conversation Conditions tutorial mostly shows how to write a conversation in the Dialogue Editor. Do you want to create a conversation in C# instead? (If so, see here.)

However, you mention the Dialogue Manager GameObject. Do you want to change settings on the Dialogue Manager? If so, then in C# change the values in DialogueManager.instance. Most of the settings you may be interested in, such as subtitle and input settings, are in DialogueManager.displaySettings.

Or are you talking about the Dialogue System Trigger? If so, see the API reference here.)
ThierryS
Posts: 5
Joined: Wed Nov 11, 2020 4:28 pm

Re: Controlling the Dialogue System asset using script C#

Post by ThierryS »

Hello Tony,
Thank you for your answer, I see now the flexibility of your Dialogue System!

The context of my requirement

The conversations messages (questions and possible responses) and the branching/conditional logic are in a “black box” (driven from a remote server). The number of questions and the possible answers are not fixed (depend on the conversations on the remote server).
So I cannot manually fill in the field into the Dialog Manager, this process has to be done dynamically (automatically) using the data on the remote server.

The different tasks that I identified are:
1) Access the conversations messages remotely using JSON requests
2) Extract/Parse from the data collected (from the remote server) which include the conversations messages, the actors, the conditions and the points allowed for each selected responses
3) Define a “usable structure” to be compatible to the Dialogue System
The idea is to also to use the editor including in the Dialogue System to visualize and check the conversations flow and the conditional logic.
4) Used Dialogue System Trigger to synchronized conversations depending on the Player action (e.g. pressed button, selected response from the conversation)


STRATEGY 1 (Point 3)
- To be compatible with the Dialogue System, create a CSV file with the conversations fields
- Use command(s) to import CSV file to build the DialogueDatabase
- Assign the DialogueDatabase to the Dialogue Editor by command

STRATEGY 2 (Point 3)
- Read every line of the data and create the conversation in C#

QUESTIONS
  • From your first reply, I used the example scene DS_RuntimeConversationExample_2020-07-31.unitypackage for creating a conversation in C#, I did not find the DialogueDatabase asset newsly created by the command “DialogueManager.AddDatabase(database)” and the DialogueManager GameObject disappeared after running the script… Is it correct?
  • What do you thing about the strategies above, do you have a better one?
  • Concerning the Dialogue System Trigger (point 4 above), the conversations will be started by the scene loader or when the pressing Button GameObject in Unity, What is needed is to save the state of the conversations (when pausing or quitting the game). When the game is restarted, I would like to have the option to reload/continue the last conversations in Unity. Do you have some advice for me?
  • Unity GameObjects will be triggered by the responses of the Player during the conversations or by the end of the current conversation (e.g. load new scene after end of the conversation), I have to use the Dialogue System Trigger to do that, isn't it ?
Sorry about the lenght of my message :D
User avatar
Tony Li
Posts: 21678
Joined: Thu Jul 18, 2013 1:27 pm

Re: Controlling the Dialogue System asset using script C#

Post by Tony Li »

ThierryS wrote: Sat Nov 14, 2020 9:53 amFrom your first reply, I used the example scene DS_RuntimeConversationExample_2020-07-31.unitypackage for creating a conversation in C#, I did not find the DialogueDatabase asset newsly created by the command “DialogueManager.AddDatabase(database)” and the DialogueManager GameObject disappeared after running the script… Is it correct?
That's correct. Because the database is generated at runtime, there is no need to save it as an asset file.
ThierryS wrote: Sat Nov 14, 2020 9:53 amWhat do you thing about the strategies above, do you have a better one?
That is a fine strategy. If your remote server can generate XML files in Chat Mapper or articy:draft format, you can also import the XML into a dialogue database fairly easily. You may find that easier than creating the database manually in code. But the key in that case is that you must be able to generate the proper XML format. If you can't generate the XML format easily, you may prefer to create the database manually in code.
ThierryS wrote: Sat Nov 14, 2020 9:53 amConcerning the Dialogue System Trigger (point 4 above), the conversations will be started by the scene loader or when the pressing Button GameObject in Unity, What is needed is to save the state of the conversations (when pausing or quitting the game). When the game is restarted, I would like to have the option to reload/continue the last conversations in Unity. Do you have some advice for me?
A dialogue database object is completely serializable. After you create it, you can serialize it to a string using JsonUtility. When resuming the game, you can deserialize the database from the string and add it back into memory. Example:

Code: Select all

string s = JsonUtility.ToJson(myDatabase);
PlayerPrefs.SetString("CurrentDB", s);
and

Code: Select all

string s= PlayerPrefs.GetString("CurrentDB");
if (!string.IsNullOrEmpty(s))
{
    myDatabase = JsonUtility.FromJson<DialogueDatabase>(s);
    if (myDatabase != null)
    {
        DialogueManager.AddDatabase(myDatabase);
    }
}
ThierryS wrote: Sat Nov 14, 2020 9:53 amUnity GameObjects will be triggered by the responses of the Player during the conversations or by the end of the current conversation (e.g. load new scene after end of the conversation), I have to use the Dialogue System Trigger to do that, isn't it ?
No. I recommend using sequencer commands. For example, when you create a dialogue entry, you can use the LoadLevel() sequencer command in the Sequence field:

Code: Select all

dialogueEntry.MenuText = "I'm finished. Go to the next scene.";
dialogueEntry.Sequence = "LoadLevel(MyNextScene)";
ThierryS
Posts: 5
Joined: Wed Nov 11, 2020 4:28 pm

Re: Controlling the Dialogue System asset using script C#

Post by ThierryS »

Great thanks Tony for your help! :D
User avatar
Tony Li
Posts: 21678
Joined: Thu Jul 18, 2013 1:27 pm

Re: Controlling the Dialogue System asset using script C#

Post by Tony Li »

Happy to help!
Sharzia
Posts: 9
Joined: Thu Nov 28, 2019 8:38 pm

Re: Controlling the Dialogue System asset using script C#

Post by Sharzia »

Hello,

I wish to save the conversation state with code and used the code examples you gave in this thread. it seems to save fine (I mean there is no error) but when loading I get an error :
ArgumentException: Cannot deserialize JSON to new instances of type 'DialogueDatabase.'
UnityEngine.JsonUtility.FromJson (System.String json, System.Type type) (at <1386288601af43018501cce2912f52f4>:0)
UnityEngine.JsonUtility.FromJson[T] (System.String json) (at <1386288601af43018501cce2912f52f4>:0)


I also tried the recorddata and applydata you mentionned in another thread, but no luck, I probabky did it wrong btw :/

Maybe I did something wrong :x (it's most likely the case), I also added all the save components to the dialogue Manager.
I'm using easy save, I also added the easysave component : ES Saved Game Data Storer.

Saving and loading is a real struggle for me, so any help is welcome :x
Thank you in advance,
User avatar
Tony Li
Posts: 21678
Joined: Thu Jul 18, 2013 1:27 pm

Re: Controlling the Dialogue System asset using script C#

Post by Tony Li »

Hi,

No worries! Saving and loading is complex, which is why the Dialogue System implements it natively, since it has to tie fairly deeply to the variable values, quest states, and conversation states.

If you wish to save the conversation state in saved games, just add a Conversation State Saver component to the Dialogue Manager GameObject. (Make sure to also add a Dialogue System Saver component.)

If you wish to save the current position of the conversation but not in a saved game, record the current conversation ID, entry ID, and transforms of the participants. Example:

Code: Select all

// Save the current conversation position:
int entryID = DialogueManager.currentConversationState.subtitle.dialogueEntry.id;
int conversationID = DialogueManager.currentConversationState.subtitle.dialogueEntry.conversationID;
string conversationTitle = DialogueManager.masterDatabase.GetConversation(conversationID).Title;
Transform actorTransform = DialogueManager.currentActor;
Transform conversantTransform = DialogueManager.currentConversant;

// Stop the conversation:
DialogueManager.StopConversation();
...
// Resume the conversation:
DialogueManager.StartConversation(conversationTitle, actorTransform, conversantTransform, entryID);
Sharzia
Posts: 9
Joined: Thu Nov 28, 2019 8:38 pm

Re: Controlling the Dialogue System asset using script C#

Post by Sharzia »

Thanks for your reply.
I should do something wrong :/
I can't get it to work to save the conversation. I couldn't make the dialogue system save to work, it's why I bought the easy save. I'm a total noob :|

I added ES saver Game data storer, JsonData serializer, dialogue system Saver, Save System and conversation state saver to my dialogue manager.
I use buttons to save to slots. I managed to save and load my variables, but the dialogue is giving me trouble. I don't have Actors per say. It's more an empty Game Objet as narrator.

Apparently it saves something , i have the following in my save file :
"conversationID" : {
"__type" : "int",
"value" : 1
},
"entryID" : {
"__type" : "int",
"value" : 16
},
"conversationTitle" : {
"__type" : "string",
"value" : "New Conversation 1"
but other than that, i'm lost. :/
User avatar
Tony Li
Posts: 21678
Joined: Thu Jul 18, 2013 1:27 pm

Re: Controlling the Dialogue System asset using script C#

Post by Tony Li »

That looks correct.

Please list the actions you are doing, what you expect to happen, and what does not happen. For example:

1. Play scene.
2. Start conversation. Progress to third dialogue entry node.
3. Save game while conversation is active.
4. Stop play mode.
5. Restart play mode.
6. Load game.
  • Expect: Conversation resumes at third dialogue entry node.
  • What Actually Happens: Conversation does not resume at all.
Also, does the Console window show any error messages or warnings?
Post Reply