Adding quests in scripts

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
mandi
Posts: 77
Joined: Wed Sep 16, 2015 4:05 am

Adding quests in scripts

Post by mandi »

Hello Tony,
I want to make a dynamic quest, where the player has to kill certain number of monsters (the number is not arbitrary, may vary depending on the spawner). I use events to track the number of spawned and killed monsters. Now, I want to feed this info to the quest I generate at runtime. The problem is the quest does not appear as tracked, although it is set as such - I don't even know whether it was generated (where to check this?).

My code looks like this:

Code: Select all

void SetKillQuest()
	{
		QuestLog.AddQuest (mKillQuestName, mKillQuestDesc);
		QuestLog.SetQuestState (mKillQuestName, PixelCrushers.DialogueSystem.QuestState.Active);
		QuestLog.SetQuestTracking (mKillQuestName, true);
	}

	void UpdateKillQuest()
	{
		mKillQuestDesc = "Kill " + (DialogueLua.GetVariable ("NoEnemiesToKill").AsInt - DialogueLua.GetVariable ("NoEnemiesKilled").AsInt) + " enemies.";
		QuestLog.SetQuestDescription (mKillQuestName, QuestState.Active, mKillQuestDesc);

		if (DialogueLua.GetVariable("NoEnemiesKilled").AsInt >= DialogueLua.GetVariable("NoEnemiesToKill").AsInt )
			QuestLog.SetQuestState (mKillQuestName, PixelCrushers.DialogueSystem.QuestState.Success);
	}

User avatar
Tony Li
Posts: 22107
Joined: Thu Jul 18, 2013 1:27 pm

Re: Adding quests in scripts

Post by Tony Li »

Hi,

Quests have two fields related to tracking:
  • Trackable: Specifies tracking is allowed to be turned on or off. If Trackable is false, then this quest cannot be tracked.
  • Track: Specifies whether to currently track the quest in the quest tracker HUD. (Trackable must also be true in this case.)
In version 1.6.1.1 and earlier, the QuestLog.SetQuestTracking() method does not set Trackable. It only sets Track. I realized recently that, while there is a method to check the value of Trackable (QuestLog.IsQuestTrackingAvailable), there was no equivalent method to set Trackable. I've already added this in the patch available on the Pixel Crushers customer download site.

If you don't want to import the patch, you can add this line to your code:

Code: Select all

void SetKillQuest()
{
      QuestLog.AddQuest (mKillQuestName, mKillQuestDesc);
      QuestLog.SetQuestState (mKillQuestName, PixelCrushers.DialogueSystem.QuestState.Active);
      
      DialogueLua.SetQuestField (mKillQuestName, "Trackable", true); //<-- ADD THIS.
      
      QuestLog.SetQuestTracking (mKillQuestName, true);
} 
If you have a quest log window, you can open it to see if the quest is listed in the Active quests section. If Trackable is not true, it won't have a Track toggle button.

You can also check the internal values of the quest by using a Lua Console. For example, if your quest is called "Kill 3 Orcs", enter this in the Console:

Code: Select all

return Quest["Kill_3_Orcs"]
I like to put counters in quest entries so they appear in the HUD underneath the main quest header. It's just personal preference. If you'd like to do the same, you can add code like this:

Code: Select all

void SetKillQuest()
{
      QuestLog.AddQuest (mKillQuestName, mKillQuestDesc);
      QuestLog.SetQuestState (mKillQuestName, PixelCrushers.DialogueSystem.QuestState.Active);
      QuestLog.SetQuestTracking (mKillQuestName, true);
      
      // ADD THIS:
      DialogueLua.SetVariable("NoEnemiesToKill", 3);
      DialogueLua.SetVariable("NoEnemiesKilled", 0);
      QuestLog.AddQuestEntry(mKillQuestName, "Kill [lua(Variable['NoEnemiesToKill']-Variable['NoEnemiesKilled'])] enemies.");
      QuestLog.SetQuestEntryState(mKillQuestName, 1, QuestState.Active);
}

void UpdateKillQuest()
{
       // ADD THIS to tell the quest tracker HUD to update its display:
       DialogueManager.SendUpdateTracker();

       if (DialogueLua.GetVariable("NoEnemiesKilled").AsInt >= DialogueLua.GetVariable("NoEnemiesToKill").AsInt)
           QuestLog.SetQuestState(mKillQuestName, PixelCrushers.DialogueSystem.QuestState.Success);
} 
mandi
Posts: 77
Joined: Wed Sep 16, 2015 4:05 am

Re: Adding quests in scripts

Post by mandi »

Tony,
splendid explanation, as usual.
Thanks!
mandi
Posts: 77
Joined: Wed Sep 16, 2015 4:05 am

Re: Adding quests in scripts

Post by mandi »

I've done what you described, but still can't see my quest as trackable. I've tried to appy patch - is there a new method in there for setting the quest trackable?
Best
User avatar
Tony Li
Posts: 22107
Joined: Thu Jul 18, 2013 1:27 pm

Re: Adding quests in scripts

Post by Tony Li »

Hi Artur,

The code in my previous post should work.

Please add a Lua Console to your scene. Set the Width and Height to 512.

After your script creates the quest, press ~+L to open the console, and then enter:

Code: Select all

return Quest["Your_Quest_Name"]
where "Your_Quest_Name" is the name of your quest, with underscores ( _ ) instead of blank spaces.

Post a screenshot of the output. I'd like to check whether the "Trackable" and "Track" fields are both being created and set.
mandi
Posts: 77
Joined: Wed Sep 16, 2015 4:05 am

Re: Adding quests in scripts

Post by mandi »

Tony,
sorry for the delay, I will check that soon and let you know.
Best!
mandi
Posts: 77
Joined: Wed Sep 16, 2015 4:05 am

Re: Adding quests in scripts

Post by mandi »

Tony,
I was finally able to check it. It works. Just my quest tracker got somehow disabled and I couldn't see any quest being tracked, only in that scene.
Best!
User avatar
Tony Li
Posts: 22107
Joined: Thu Jul 18, 2013 1:27 pm

Re: Adding quests in scripts

Post by Tony Li »

Just making sure - it's all working correctly now?
mandi
Posts: 77
Joined: Wed Sep 16, 2015 4:05 am

Re: Adding quests in scripts

Post by mandi »

Tony,
yes, it does. Sorry for the confusion.
Best!
User avatar
Tony Li
Posts: 22107
Joined: Thu Jul 18, 2013 1:27 pm

Re: Adding quests in scripts

Post by Tony Li »

Great! I'm just glad to hear it's working. :-)
Post Reply