Page 1 of 2

How To: Add Custom Fields

Posted: Wed Apr 01, 2020 8:25 am
by Tony Li
There are several ways to customize the type of content in your dialogue database. Here are some.

Variable Scope
Variables in the Dialogue System are global. Some designers add "scope" by including it at the front of the variable name, such as:
  • France.Population
  • France.GDP
  • France.Official_Language
  • Spain.Population
  • Spain.GDP
  • Spain.Official_Language
You can set the filter to "France.", for example, to see only the variables in the France "scope".

In version 2.2.5+, variables with "." are shown as submenus in dropdowns:

Image


Custom Fields
You can also add your own fields to a quest. In the example below, I added a field named "XP" to a quest. You can also add this field to the Quests template on the Templates tab so it gets added to all quests.

Image

To get the value of this field in your own C# scripts:

Code: Select all

int XPReward = DialogueLua.GetQuestField("Get the Launch Codes", "XP").asInt;

Custom Field Types
You can also create your own custom field types. In the screenshot below, I created a new field type named "Mission Type". It has 3 choices: Main, Side, or Companion.

Image

To create your own type, copy the template in Plugins/Pixel Crushers/Dialogue System/Templates/Scripts/Editor. There is also another example type in there, as well as another in Plugins/Pixel Crushers/Dialogue System/Scripts/Editor/Fields/Example.

The code for the Mission Type is here:

Code: Select all

using UnityEngine;
using UnityEditor;
using System;

namespace PixelCrushers.DialogueSystem
{

    public enum MissionType
    {
        Main,
        Side,
        Companion
    }

    [CustomFieldTypeService.Name("Mission Type")]
    public class QuestType : CustomFieldType
    {

        public override string Draw(string currentValue, DialogueDatabase dataBase)
        {
            var enumValue = GetCurrentMissionType(currentValue);
            return EditorGUILayout.EnumPopup(enumValue).ToString();
        }

        public override string Draw(Rect rect, string currentValue, DialogueDatabase dataBase)
        {
            var enumValue = GetCurrentMissionType(currentValue);
            return EditorGUI.EnumPopup(rect, enumValue).ToString();
        }

        private MissionType GetCurrentMissionType(string currentValue)
        {
            if (string.IsNullOrEmpty(currentValue)) currentValue = MissionType.Main.ToString();
            try
            {
                return (MissionType)Enum.Parse(typeof(MissionType), currentValue, true);
            }
            catch (Exception)
            {
                return MissionType.Main;
            }
        }

    }
}
Internally, it's stored as a string, so you can simply do a string check in your own scripts:

Code: Select all

bool isSideMission = DialogueLua.GetQuestField("Get the Launch Codes", "Mission Type").asString;

Re: How To: Add Custom Fields

Posted: Thu Apr 02, 2020 1:37 pm
by Stalker_EG
Tell me, do I need to re-import playMaker support after the 2.2.5 update?
I'm trying to change a variable with "." using PlayMaker Action "Set Variable". It doesn't work!

Re: How To: Add Custom Fields

Posted: Thu Apr 02, 2020 1:51 pm
by Tony Li
Hi,

You shouldn't have to, but it won't hurt to import the latest version from the Third Party Support folder.

Are there any errors or warnings in the Console window?

Re: How To: Add Custom Fields

Posted: Thu Apr 02, 2020 2:11 pm
by Stalker_EG
the variable is in the Variables table.
its name is Dan.DailyReading.

When trying to change it. the console receives a record that the variable DailyReading. Not found.
It's DailyReading without "Dan."

But when I change ". " to "/" everything works.

Re: How To: Add Custom Fields

Posted: Thu Apr 02, 2020 2:36 pm
by Tony Li
What versions of Unity and PlayMaker are you using?

Re: How To: Add Custom Fields

Posted: Thu Apr 02, 2020 2:53 pm
by Stalker_EG
Unity 2019.3.5f1 (64-bit)
PM Version: 1.9.0.p20 • Feb 19, 2020

Re: How To: Add Custom Fields

Posted: Thu Apr 02, 2020 3:25 pm
by Tony Li
Hi,

Is this similar to how you set it up?

I defined a variable in the Dialogue Editor:

playMakerDotVar1.png
playMakerDotVar1.png (9.78 KiB) Viewed 1367 times

Then I used the Set Variable action in a PlayMaker FSM to change its value:

playMakerDotVar2.png
playMakerDotVar2.png (7.47 KiB) Viewed 1367 times

And it seems to work fine.

Please feel free to send a reproduction project to tony (at) pixelcrushers.com if you'd like me to take a look at it directly.

Re: How To: Add Custom Fields

Posted: Thu Apr 02, 2020 4:06 pm
by Stalker_EG
Yeah, that's right. I have the same data. And it looks like I've already seen my mistake. I was trying to assign an INT value. Now I'm going to check if Float is working.

Re: How To: Add Custom Fields

Posted: Sun May 10, 2020 2:17 pm
by davidisak
Hi!

I'm trying to make a CustomFieldType from an enum and have some problems with its appearance in the editor.
Currently I've just copied your MissionType code example but I don't get any dropdown list to select from in the editor, it only shows textfields as can be seen in the attached image.
There are no errors or warnings showing.
I'm using Unity 2019.3.9f1 and the evaluation version of the Dialogue System v 2.2.6.
I've tried doing this in a clean Unity project with the same result.

Any help is very much appreciated.

Image

Re: How To: Add Custom Fields

Posted: Sun May 10, 2020 2:45 pm
by Tony Li
Hi,

Is your script (e.g., CustomFieldType_MissionType.cs) in a folder named "Editor"? That's a requirement since it modifies the editor.