Page 1 of 1

How to abandon a quest programatically?

Posted: Tue Apr 23, 2024 3:47 am
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);
        }
    }
}


Re: How to abandon a quest programatically?

Posted: Tue Apr 23, 2024 4:42 am
by dkrusenstrahle
Nevermind, I forgot to check the box "abandonable". Works now.