Prevent QuestJournal from adding the same Quest

Announcements, support questions, and discussion for Quest Machine.
Post Reply
keattikorn
Posts: 3
Joined: Fri Jul 29, 2022 2:44 pm

Prevent QuestJournal from adding the same Quest

Post by keattikorn »

How can I prevent quest being added to the QuestJournal that has already contained the quest being added.
User avatar
Tony Li
Posts: 21925
Joined: Thu Jul 18, 2013 1:27 pm

Prevent QuestJournal from adding the same Quest

Post by Tony Li »

Hi,

How are you adding the quest?

A Quest Giver's dialogue UI won't offer a quest if either of these are true:

1. The player already has an active hand-written quest with the same ID.

2. Or the player already has a procedurally-generated quest with the same target and action (e.g., "kill" + "orcs").


If you're manually calling QuestGiver.GiveQuestToQuester() in C#, you can do a similar check before calling that method.


Alternatively, if you want to block it on the QuestJournal side, you can make a subclass of QuestJournal that overrides AddQuest(Quest). Something like:

Code: Select all

public class MyQuestJournal : QuestJournal
{
    public void AddQuest(Quest quest)
    {
        if (/* player already has same quest according to your criteria */)
        {
            Quest.DestroyInstance(quest);
        }
        else        
        {
            base.AddQuest(quest);
        }
    }
}
keattikorn
Posts: 3
Joined: Fri Jul 29, 2022 2:44 pm

Re: Prevent QuestJournal from adding the same Quest

Post by keattikorn »

I do add quest by scripting following your article on add quest by script.

At the moment I check the duplication by checking the quest ID being added.

however, these issues are still have to be addressed.

- the identical 2 quests still can be added if I add a quest as an Action on a successful node "Give Quest to Quester", this action does not check for duplication. Although, the duplication is fine on a QuestJournal component and the journal can be saved to disk successfully with the duplication (I have already inspected the saved JSON content). However, when it is time to load the saved game back, I can see from the load save game mechanic that it uses quest ID as a key in a dictionary, therefore, the result of the duplicated quests in the saved game will not be loaded properly (only one quest will be loaded).

- another issue is that when the quest is completed or abandoned, it is still in the journal database, this can have the issue that the new quest instance cannot be added because the quest with the same ID (although it is completed or abandoned) is being added.

Code: Select all

           
            var questInstance = m_QuestBeingAdded.Clone();
            
            if (!m_QuestJournal.ContainsQuest(questInstance.id))
            {
                // Add the copy to the quester and activate it:
                var questerTextInfo = new QuestParticipantTextInfo(
                    m_QuestJournal.id, m_QuestJournal.displayName, m_QuestJournal.image, null);
                questInstance.AssignQuester(questerTextInfo);
                questInstance.timesAccepted = 1;
                m_QuestJournal.deletedStaticQuests.Remove(StringField.GetStringValue(questInstance.id));
                m_QuestJournal.AddQuest(questInstance);
                questInstance.SetState(QuestState.Active);
                QuestMachineMessages.RefreshUIs(questInstance);
            }
User avatar
Tony Li
Posts: 21925
Joined: Thu Jul 18, 2013 1:27 pm

Re: Prevent QuestJournal from adding the same Quest

Post by Tony Li »

Hi,

Since you're adding the quest manually (by script and/or by quest action), you'll need to check if the quest is already in the journal. If the quest is in the journal but completed or abandoned, delete it using QuestJournal.DeleteQuest(). If you're using a quest action, you can subclass GiveQuestToQuesterQuestAction and override the Execute() method to check.

If you want the quest journal to always remove quests them when are completed or abandoned, UNtick the Quest Journal component's Save Settings > Remember Completed Quests.

If you only want specific quests to remove themselves from the journal when completed or abandoned, inspect the quest in the Quest Editor window. In the main quest info section, tick Delete When Complete.
keattikorn
Posts: 3
Joined: Fri Jul 29, 2022 2:44 pm

Re: Prevent QuestJournal from adding the same Quest

Post by keattikorn »

Thanks for your answer. I will try your solution.
User avatar
Tony Li
Posts: 21925
Joined: Thu Jul 18, 2013 1:27 pm

Re: Prevent QuestJournal from adding the same Quest

Post by Tony Li »

Glad to help!
Post Reply