Creating conversation in Editor through script

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
forrestuv
Posts: 64
Joined: Tue Nov 06, 2018 11:28 am

Creating conversation in Editor through script

Post by forrestuv »

Hi,
I need to create conversations through editor script.
This conversation must have 2 entries (and 1 startnode)
I create the 2 entries and add them to conversation.
Everything works fine, except the start node that is created with an ID succesive to the previous 2 entries.
What's the correct procedure to create conversation with a startnode with id=0?
I've also tried creating the startnode myself setting id=0 and root=true but another startnode gets created automatically.

Code: Select all

Conversation conv = new Conversation();

            //DialogueEntry de_start = new DialogueEntry();
            //de_start.isRoot = true;
            //de_start.id = 0;
            //de_start.fields = new List<Field>();
            //de_start.fields.Add(new Field("Title", string.Empty, FieldType.Text));
            //de_start.fields.Add(new Field("Description", string.Empty, FieldType.Text));
            //de_start.fields.Add(new Field("Actor", string.Empty, FieldType.Actor));
            //de_start.fields.Add(new Field("Conversant", string.Empty, FieldType.Actor));
            //de_start.fields.Add(new Field("Dialogue Text", string.Empty, FieldType.Text));
            //de_start.Title = "Start";

            DialogueEntry de_name = new DialogueEntry();
            de_name.id = 1;
            de_name.fields = new List<Field>();
            de_name.fields.Add(new Field("Title", string.Empty, FieldType.Text));
            de_name.fields.Add(new Field("Description", string.Empty, FieldType.Text));
            de_name.fields.Add(new Field("Actor", string.Empty, FieldType.Actor));
            de_name.fields.Add(new Field("Conversant", string.Empty, FieldType.Actor));
            de_name.fields.Add(new Field("Dialogue Text", string.Empty, FieldType.Text));

            DialogueEntry de_description = new DialogueEntry();
            de_description.id = 2;
            de_description.fields = new List<Field>();
            de_description.fields.Add(new Field("Title", string.Empty, FieldType.Text));
            de_description.fields.Add(new Field("Description", string.Empty, FieldType.Text));
            de_description.fields.Add(new Field("Actor", string.Empty, FieldType.Actor));
            de_description.fields.Add(new Field("Conversant", string.Empty, FieldType.Actor));
            de_description.fields.Add(new Field("Dialogue Text", string.Empty, FieldType.Text));

            conv.fields = new List<Field>();
            conv.fields.Add(new Field("Title", string.Empty, FieldType.Text));
            conv.fields.Add(new Field("Description", string.Empty, FieldType.Text));
            conv.fields.Add(new Field("Actor", string.Empty, FieldType.Actor));
            conv.fields.Add(new Field("Conversant", string.Empty, FieldType.Actor));
            conv.fields.Add(new Field("Menu Text", string.Empty, FieldType.Text));
            conv.fields.Add(new Field("Dialogue Text", string.Empty, FieldType.Text));
            conv.Title = "MyTitle;
            ds.initialDatabase.AddConversation(conv);

            //conv.dialogueEntries.Add(de_start);
            conv.dialogueEntries.Add(de_name);
            conv.dialogueEntries.Add(de_description);
        }
        ds.ResetDatabase(DatabaseResetOptions.KeepAllLoaded);
        EditorUtility.SetDirty(db);
User avatar
Tony Li
Posts: 22135
Joined: Thu Jul 18, 2013 1:27 pm

Re: Creating conversation in Editor through script

Post by Tony Li »

Hi,

Use the Template class like in How To: Create Dialogue Database At Runtime except instead of creating a default Template object with Template.FromDefault(), you can use any custom template settings you've already set up in the Dialogue Editor by creating a Template object with TemplateTools.LoadFromEditorPrefs().

In addition to the code in the How To link, you'll also want to use the UnityEditor.AssetDatabase class to save the database as an asset.
Post Reply