[HOWTO] How To: Add Custom Data To Quests

Announcements, support questions, and discussion for Quest Machine.
Post Reply
User avatar
Tony Li
Posts: 22112
Joined: Thu Jul 18, 2013 1:27 pm

[HOWTO] How To: Add Custom Data To Quests

Post by Tony Li »

This post describes some ways to add custom data to a quest, beyond what you can add in the Quest Editor and by adding custom quest conditions, actions, and UI content. (In most cases, adding data in the Quest Editor and/or custom conditions, actions, and UI content is more than sufficient.)

While you you cannot derive subclasses from Quest, you can add data to its tagDictionary. Example use:

Code: Select all

quest.tagDictionary.AddTag("Difficulty", "Hard");
if (quest.tagDictionary.ContainsTag("Difficulty"))
{
    Debug.Log("This quest is: " + quest.tagDictionary.GetTag("Difficulty", "Medium"); // If no tag, assume Medium.
}
For procedural quests, you can do two things:

1. Assign a delegate method to QuestGeneratorEntity.generatedQuest. This method will be called after generating the quest. You can use it to add more data or otherwise adjust the quest.

Side note: You can also assign a delegate method to QuestGeneratorEntity.updateWorldModel to add more information to the world model than what it normally sees in its Quest Domains. This method is called just before sending the world model to the quest generator.

2. Or derive a subclass of PlanToQuestBuilder and assign it to QuestGeneratorEntity.questGenerator.planToQuestBuilder.

When an NPC generates a quest, it follows these steps:
  • Builds a world model.
  • Passes the world model to the quest generator, which determines a sequence of Actions to take (called a plan).
  • Passes the plan to PlanToQuestBuilder, which turns the Actions into a Quest object and returns it to the NPC.
  • The NPC adds the quest to its Quest Giver component's Quests list.
User avatar
Tony Li
Posts: 22112
Joined: Thu Jul 18, 2013 1:27 pm

Re: How To: Add Custom Data To Quests

Post by Tony Li »

Another note:

Although tagDictionary stores strings, you can store any data by using JsonUtility:

Code: Select all

[Serializable]
public class MyCustomData
{
    public bool aBool;
    public float aFloat;
}
public MyCustomData myCustomData;

tagDictionary.SetTag("CustomData", JsonUtility.ToJson(myCustomData));
User avatar
Tony Li
Posts: 22112
Joined: Thu Jul 18, 2013 1:27 pm

Re: How To: Add Custom Data To Quests

Post by Tony Li »

To change the format of procedurally-generated return node text, make a subclass of PlanToQuestBuilder and assign it to QuestGeneratorEntity.questGenerator.planToQuestBuilder. In your subclass, override the AddReturnNodeText() method. Example: (switches order of "{Return to}" and quest giver name.

Code: Select all

public class CustomPlanToQuestBuilder : PlanToQuestBuilder
{
    protected override void AddReturnNodeText(QuestBuilder questBuilder, QuestNode returnNode, QuestGiver questGiver, string mainTargetEntity, string mainTargetDescriptor, string domainName, PlanStep goal, string hudText)
    {
        // Use the base method first to set up the text objects:
        base.AddReturnNodeText(questBuilder, returnNode, questGiver, mainTargetEntity, mainTargetDescriptor, domainName, goal, hudText);

        // If the current language is Korean, change the journal text and HUD text:
        if (UILocalizationManager.instance.currentLanguage == "ko")
        {
            // Change the journal text:
            var journalBodyText = journalList.contentList[journalList.contentList.Count - 1] as BodyTextQuestContent;
            journalBodyText.bodyText.value = questGiver.displayName + " {Return to}.";

            // Change the HUD text:
            var hudBodyText = hudList.contentList[hudList.contentList.Count - 1] as BodyTextQuestContent;
            hudBodyText.bodyText.value = questGiver.displayName + " {Return to}";
        }
    }
}

Code: Select all

GetComponent<QuestGeneratorEntity>().planToQuestBuilder = new CustomPlanToQuestBuilder();
Post Reply