[HOWTO] How To: Start Quest From Script

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

[HOWTO] How To: Start Quest From Script

Post by Tony Li »

This post explains how to give a quest without using a QuestGiver.

The most common way to give a quest to a quester (e.g., player) is to use a QuestGiver component. You can either call QuestGiver.StartDialogueWithPlayer() to show the accept/decline UI, or call QuestGiver.GiveQuestToQuester() (or the equivalent visual scripting action) to give it directly without using UI. Note that you can add a QuestGiver to an empty GameObject. It doesn't have to have a visual representation in the scene.

However, if you don't want to use a QuestGiver, you can give the quest directly to the player and activate it using a few lines of scripting. Here's an example script that gives a quest to a player. It takes care of all of the housekeeping of clearing the player's quest journal of old copies of the quest, activating the quest properly so its listeners also activate, etc.

GiveQuest.cs

Code: Select all

using UnityEngine;
using PixelCrushers;
using PixelCrushers.QuestMachine;

public class GiveQuest : MonoBehaviour
{
    public Quest questAsset;
    public QuestJournal questJournal;

    void Start()
    {
        // Make a copy of the quest for the quester:
        var questInstance = questAsset.Clone();

        // Add the copy to the quester and activate it:
        var questerTextInfo = new QuestParticipantTextInfo(questJournal.id, questJournal.displayName, questJournal.image, null);
        questInstance.AssignQuester(questerTextInfo);
        questInstance.timesAccepted = 1;
        questJournal.deletedStaticQuests.Remove(StringField.GetStringValue(questInstance.id));
        questJournal.AddQuest(questInstance);
        questInstance.SetState(QuestState.Active);
        QuestMachineMessages.RefreshUIs(questInstance);
    }
}

Alternatives
  • At design time, you can assign the quest to the player's Quest Journal component > Quests list. In this case, you'll probably want to set the quest state and initial quest nodes' states to Active, or configure Autostart Conditions.
  • Or add a Quest Giver component to an empty GameObject. (Quest Giver components don't always have to be on visible NPCs.) Then call methods on it such as QuestGiver.GiveAllQuestsToPlayer().
mac
Posts: 81
Joined: Sat Aug 22, 2020 7:54 pm

Re: How To: Start Quest From Script

Post by mac »

Thank you Tony, I successfully added a quest with the script you provided, but I noticed the quest did not go to my active quest HUD on the top right, how can I also assign the quest to the HUD? Thanks in advance.
User avatar
Tony Li
Posts: 21987
Joined: Thu Jul 18, 2013 1:27 pm

Re: How To: Start Quest From Script

Post by Tony Li »

Hi,

Inspect your quest's main properties by editing it in the Quest Editor window and clicking on blank canvas space. Make sure Quest Info > Is Trackable and Show In Track HUD are both ticked.

Then check that you've added content to States > Active > HUD Text. You can add HUD Text content to the main quest properties and/or quest nodes.

Also change the last line in the script above from RefreshIndicators to RefreshUIs. (I just updated the post.) RefreshIndicators will only tell overhead indicators to refresh. RefreshUIs will also tell the quest HUD and quest journal UI to refresh.
Post Reply