How to abandon a quest programatically?

Announcements, support questions, and discussion for Quest Machine.
Post Reply
dkrusenstrahle
Posts: 36
Joined: Sat Apr 20, 2024 7:03 pm

How to abandon a quest programatically?

Post by dkrusenstrahle »

Hello,

How can I abondon a quest programatically?
I am printing out my currently accepted quests in my own UI canvas using the script below
and the button is connected to the AbandonQuest method.

Both Journal and selectedQuest is set and I get no errors but the quest is not abandoned.

Code: Select all

using UnityEngine;
using UnityEngine.UI;
using System.Collections.Generic;
using PixelCrushers.QuestMachine;

public class UIQuestsList : MonoBehaviour
{
    [SerializeField] private GameObject itemPrefab;
    [SerializeField] private Transform listParent;

    private void OnEnable()
    {
        ClearList();
        GenerateReport();
    }

    private void GenerateReport()
    {
        QuestJournal journal = QuestMachine.GetQuestJournal();
        foreach (Quest quest in journal.questList)
        {
            GameObject itemRow = Instantiate(itemPrefab, listParent);
            itemRow.transform.Find("QuestName").GetComponent<Text>().text = $"{quest.title}";
            itemRow.transform.Find("QuestGiver").GetComponent<Text>().text = $"{quest.questGiverID}";
            itemRow.transform.Find("Status").GetComponent<Text>().text = $"{quest.GetState()}";

            Button abandonButton = itemRow.transform.Find("AbandonButton").GetComponent<Button>();
            abandonButton.onClick.AddListener(() => AbandonQuest(quest));
        }
    }

    public void AbandonQuest(Quest selectedQuest)
    {
        QuestJournal journal = QuestMachine.GetQuestJournal();
        if (journal != null)
        {
            journal.AbandonQuest(selectedQuest);
            ClearList();
            GenerateReport();
        }
    }

    private void ClearList()
    {
        foreach (Transform child in listParent)
        {
            Destroy(child.gameObject);
        }
    }
}

dkrusenstrahle
Posts: 36
Joined: Sat Apr 20, 2024 7:03 pm

Re: How to abandon a quest programatically?

Post by dkrusenstrahle »

Nevermind, I forgot to check the box "abandonable". Works now.
Post Reply