Convert Chat Mapper XML to Database in C#

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
Rewar
Posts: 4
Joined: Fri Jul 10, 2015 5:50 am

Convert Chat Mapper XML to Database in C#

Post by Rewar »

Hello !
I must make an automatic converter which could read a ChatMapper XML file in a specified folder and convert it into a DialogueDatabase. After that, I must launch the generated dialogue in the scene. But I still don't know how to make it. I've tried to attach the DialogueDatabase to the Gameobject Dialogue Manager. It does'nt work.
This is code that I made.

Code: Select all

ChatMapperProject cmpp = ChatMapperTools.Load(@"Assets/Resources/Dialogs/source/scenar3.xml");
DialogueDatabase ddb = cmpp.ToDialogueDatabase();
DialogueSystemController dsc = GameObject.Find( "Dialogue Manager" ).GetComponent<DialogueSystemController>();
dsc.initialDatabase = ddb;
If you have a solution, it would be great to hear it. Thank you.
User avatar
Tony Li
Posts: 22102
Joined: Thu Jul 18, 2013 1:27 pm

Re: Convert Chat Mapper XML to Database in C#

Post by Tony Li »

Hello,

DialogueManager.initialDatabase is only used when the Dialogue Manager starts. To add a database later, use DialogueManager.AddDatabase():

Code: Select all

ChatMapperProject cmpp = ChatMapperTools.Load(@"Assets/Resources/Dialogs/source/scenar3.xml");
DialogueDatabase ddb = cmpp.ToDialogueDatabase();
DialogueManager.AddDatabase(ddb);
Rewar
Posts: 4
Joined: Fri Jul 10, 2015 5:50 am

Re: Convert Chat Mapper XML to Database in C#

Post by Rewar »

Thank you for that tip but how could I load the Dialogue in my scene on Start ? I've a DialogueManager gameobject and un ConversationTrigger but evenif I change their DialogueDatabase, it still not work.
User avatar
Tony Li
Posts: 22102
Joined: Thu Jul 18, 2013 1:27 pm

Re: Convert Chat Mapper XML to Database in C#

Post by Tony Li »

Hi,

If you're specifying the XML file at design time, it's easier to treat it as a TextAsset. This way you don't have to find the user's current path at runtime.

Try adding a script like this:

Code: Select all

using UnityEngine;
using System.Collections;
using PixelCrushers.DialogueSystem;

public class AddConvertedDatabase : MonoBehaviour {

    public TextAsset xmlFile; //<-- Assign your XML file here in the inspector.
    
    public IEnumerator Start() {
        yield return null; // Allow 1 frame for Dialogue Manager to initialize.
        ChatMapperProject cmpp = ChatMapperTools.Load(xmlFile);
        if (cmpp == null) {
            Debug.LogError("Can't load XML file.");
        } else {
            DialogueDatabase ddb = cmpp.ToDialogueDatabase();
            if (ddb == null) {
                Debug.LogError("Can't convert Chat Mapper project to dialogue database.");
            } else {
                DialogueManager.AddDatabase(ddb);
            }
        }
    }
} 
Rewar
Posts: 4
Joined: Fri Jul 10, 2015 5:50 am

Re: Convert Chat Mapper XML to Database in C#

Post by Rewar »

Maybe I express my idea badly. That XML file to import into my scene at the Start of the game is in a specified repository, so I don't have any problem to load it in C#.
Actually, I start the game by press Play, I loaded the file and added it to DialogueDatabase like this :

Code: Select all

ChatMapperProject cmpp = ChatMapperTools.Load(@"Assets/Resources/Dialogs/source/scenar3.xml");
DialogueDatabase ddb = cmpp.ToDialogueDatabase();
DialogueManager.AddDatabase( ddb );
But after that, I want to launch the added Dialogue. I've already a DialogueManager gameobject in my scene and a Character which have a component ConversationTrigger. How can I start the conversation in the loaded Database ?
User avatar
Tony Li
Posts: 22102
Joined: Thu Jul 18, 2013 1:27 pm

Re: Convert Chat Mapper XML to Database in C#

Post by Tony Li »

DialogueManager.AddDatabase(ddb) merges the ddb database into the Dialogue Manager's master runtime database. All conversations in scenar3.xml will be available in the master database.

Here are two different methods to start the conversation:

Method #1:
On the Conversation Trigger, click the toggle next to the conversation title. This will switch between a dropdown menu and a text field. Since the dialogue database doesn't exist at design time, you can't use the dropdown menu. Instead, enter the conversation title in the text field.

Method #2:
Don't use Conversation Trigger. Call DialogueManager.StartConversation().

Code: Select all

ChatMapperProject cmpp = ChatMapperTools.Load(@"Assets/Resources/Dialogs/source/scenar3.xml");
DialogueDatabase ddb = cmpp.ToDialogueDatabase();
DialogueManager.AddDatabase( ddb );

// Add this:
DialogueManager.StartConversation("My Conversation Title", actorTransform, conversantTransform);
Rewar
Posts: 4
Joined: Fri Jul 10, 2015 5:50 am

Re: Convert Chat Mapper XML to Database in C#

Post by Rewar »

Thank you it works. That's the second method, because I can't touch the Unity Editor, so I must code all changements in C#.

That's ok now, thank you ;)
User avatar
Tony Li
Posts: 22102
Joined: Thu Jul 18, 2013 1:27 pm

Re: Convert Chat Mapper XML to Database in C#

Post by Tony Li »

Happy to help! I'm glad it's working now.
Post Reply