database's quest inspector "State" field always show as (None) when trying to add quest through C# script

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
StunnedSnail
Posts: 3
Joined: Mon Feb 03, 2025 6:05 pm

database's quest inspector "State" field always show as (None) when trying to add quest through C# script

Post by StunnedSnail »

I'm trying to add a quest through a C# script at edit time. Here’s the snippet:

Code: Select all

    public string questTitleToAdd;
    
    public string questDescriptionToAdd;

    public QuestState questStateToAdd;

    public DialogueDatabase database;

   [ContextMenu("Add Quest")]
    public void AddAQuest()
    {
        Item item = database.GetItem(questTitleToAdd);
        if (item != null)
        {
            Debug.Log("Quest already exists");
            return;
        }

        var template = TemplateTools.LoadFromEditorPrefs();
        var quest = template.CreateQuest(template.GetNextQuestID(database), questTitleToAdd);

        quest.fields.Find(f => f.title == "Description").value = questDescriptionToAdd;
        quest.fields.Find(f => f.title == "State").value = questStateToAdd.ToString(); //without this it's show as Unassign in inspector
        database.items.Add(quest);

        //Both don't seem to do anything
        //DialogueLua.SetQuestField(quest.Name, "State", questStateToAdd.ToString());
        //DialogueLua.SetQuestField(quest.Name, "State", questStateToAdd);
        
        #if UNITY_EDITOR
                UnityEditor.EditorUtility.SetDirty(database);
                UnityEditor.AssetDatabase.SaveAssets();
        #endif

        if (DialogueEditorWindow.instance != null) DialogueEditorWindow.instance.Reset();


        //Check state value => correct
        foreach (var checkQuest in database.items)
        {
            if (checkQuest.IsItem) continue;
            var stateField = checkQuest.fields.Find(f => f.title == "State");
 	    Debug.Log($"Quest: {checkQuest.Name}, State: {stateField.value}");

        }

    }
After running this function, the quest does get added to the database under the "Quest/Items" tab. The Name and Description appear correctly in both the Inspector and the dialogue window. However, the State in the Inspector always shows as (None).

According to the debug snippet at the bottom, the printed state value is correctly assigned (it shows the value of questStateToAdd). So I think the issue was with the Inspector, but I can't figure out what’s wrong :cry: Anyone know what did I do wrong?

(By the way, this function (along with another function that read quests from database) is meant to sync (push or pull) my custom quest system with this dialogue system. My quest system is just a simple GraphView with color-coded nodes and connections that determine which quests unlock others—really easy for me to debug and get a big-picture view. But I haven’t found much information on adding quests through scripts, so I’m not sure if what I’m doing is the right approach, haha.)

Edit: I found the problem—you need to add .ToLower()

=> quest.fields.Find(f => f.title == "State").value = questStateToAdd.ToString().ToLower()
Last edited by StunnedSnail on Tue Feb 04, 2025 4:54 am, edited 1 time in total.
User avatar
Tony Li
Posts: 23256
Joined: Thu Jul 18, 2013 1:27 pm

Re: database's quest inspector "State" field always show as (None) when trying to add quest through C# script

Post by Tony Li »

Hi,

Some important points:

1. At runtime, consider the dialogue database to be read only. All runtime changes go into the Lua environment.

2. Generally speaking, the Dialogue Editor will show the original values of your database, not runtime values, with the exception of the Watches tab.

2. Use the QuestLog class to work with quests at runtime. Example:

Code: Select all

QuestLog.AddQuest("My Quest", "This is my quest description.", QuestState.Active);
StunnedSnail
Posts: 3
Joined: Mon Feb 03, 2025 6:05 pm

Re: database's quest inspector "State" field always show as (None) when trying to add quest through C# script

Post by StunnedSnail »

Tony Li wrote: Mon Feb 03, 2025 8:03 pm Hi,

Some important points:

1. At runtime, consider the dialogue database to be read only. All runtime changes go into the Lua environment.

2. Generally speaking, the Dialogue Editor will show the original values of your database, not runtime values, with the exception of the Watches tab.

2. Use the QuestLog class to work with quests at runtime. Example:

Code: Select all

QuestLog.AddQuest("My Quest", "This is my quest description.", QuestState.Active);
Hey Tony
I specifically want to modify the database at edit time, not runtime—that’s why I’m not using QuestLog.
StunnedSnail
Posts: 3
Joined: Mon Feb 03, 2025 6:05 pm

Re: database's quest inspector "State" field always show as (None) when trying to add quest through C# script

Post by StunnedSnail »

Oh, I found the problem—you need to add .ToLower().

quest.fields.Find(f => f.title == "State").value = questStateToAdd.ToString().ToLower();


When converting QuestState in the Inspector to a string, the first character is capitalized by default.
Attachments
Screenshot 2025-02-04 164858.jpg
Screenshot 2025-02-04 164858.jpg (25.74 KiB) Viewed 2337 times
User avatar
Tony Li
Posts: 23256
Joined: Thu Jul 18, 2013 1:27 pm

Re: database's quest inspector "State" field always show as (None) when trying to add quest through C# script

Post by Tony Li »

Gotcha. The Dialogue System has some popup attributes that might be handy.
Post Reply