Saving Quest progress issue

I have run into an issue with saving the updated progress. My UI is updating fine when doing a quest, both the list and the toolTip UI update accordingly. However, though testing and debugging, I have learned my questStatuses list is reset to zero… and I am not sure why.

using GameDevTV.Saving;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;


namespace RPG.Quests
{
    
    public class QuestList : MonoBehaviour, ISaveable
    {
        public delegate void OnQuestUpdated();
        public event OnQuestUpdated QuestUpdated;

        List<QuestStatus> questStatuses = new List<QuestStatus>();

        public void AddQuest(Quest quest)
        {
            if (HasQuest(quest)) return;
            QuestStatus newStatus = new QuestStatus(quest);
            questStatuses.Add(newStatus);
            print($"QuestStatus Length {questStatuses.Count} "); //<--- this prints correctly when a quest is added
            if (QuestUpdated != null)
            { QuestUpdated?.Invoke(); }

        }

        public void CompleteObjective(Quest quest, string objective)
        {
            QuestStatus status = GetQuestStatus(quest);
            status.CompleteObjective(objective);
            if (QuestUpdated != null)
            { QuestUpdated?.Invoke(); }
        }

        public IEnumerable<QuestStatus> GetStatuses()
        {
            return questStatuses;
        }

        public bool HasQuest(Quest quest)
        {
           return GetQuestStatus(quest) != null;
        }
        private QuestStatus GetQuestStatus(Quest quest)
        {
            foreach (QuestStatus status in questStatuses)
            {
                if (status.GetQuest() == quest)
                {
                    return status;
                }
            }
            return null;
        }


        //ISaveable Implementation
        public object CaptureState()
        {
            print(questStatuses.Count); //<---- This prints as zero
            print("Capture QuestList");
            List<object> state = new List<object>();
            foreach(QuestStatus status in questStatuses)
            {
                state.Add(status.CaptureState());//<--- this never prints because it is always 0.
                print($"Adding state {state}");  
            }
            return state;
        }

        public void RestoreState(object state)
        {
            Debug.Log("Restore QuestList");
            List<object> stateList = state as List<object>;
            
            if (stateList == null)
            {
                Debug.Log($"{stateList} is null");
                return;
            }
            
            foreach (object objectState in stateList)
            {
                Debug.Log($"stateList Count {stateList.Count}");
                Debug.Log($"Restoring {objectState}");
                questStatuses.Add(new QuestStatus(objectState));
               
            }
        }
    }
}

The Quest Status Script, I am not sure what I am missing…

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace RPG.Quests
{
   
    public class QuestStatus
    {
         Quest quest;
        List<string> completedObjectives = new List<string>();
        private object objectState;

        [System.Serializable]
        class QuestStatusRecord
        {
            public string questName;
            public List<string> completedObjectives;
        }


        public QuestStatus(Quest quest)
        {
            this.quest = quest;
        }

        public QuestStatus(object objectState)
        {
            Debug.LogWarning($"creating QuestStatus {objectState}");
            QuestStatusRecord state = objectState as QuestStatusRecord;
            quest = Quest.GetByName(state.questName);
            completedObjectives = state.completedObjectives;
        }

        public Quest GetQuest()
        {
            return quest;
        }

        public int GetCompletedCount()
        {
            return completedObjectives.Count;
        }

        public bool IsObjectiveComplete(string objective)
        {
            return completedObjectives.Contains(objective);
        }

        public void CompleteObjective(string objective)
        {
            if (quest.HasObjective(objective))
            {
                completedObjectives.Add(objective);
            }
        }

        public object CaptureState()
        {
            Debug.Log("Quest Status CaptureState");
            QuestStatusRecord state = new QuestStatusRecord();
            state.questName = quest.name;
            state.completedObjectives = completedObjectives;
            return state;
        }
    }
}

Well… it actually looks like my player somehow ended up with 2 Quest Lists on the prefab, which was causing the saving issue. But now, on load its not setting the tool tip information correctly any more… so here I go on that issue… :slight_smile:

So after doing some mroe testing and searching through my code, I found a slight misstatement in the GetByName function, and When i went back to an earlier commit, to check possible errors. I guess my resources folder got undone, so after some more testing, and making sure Ive got the mistakes cleared up. things are functioning and saving correctly! :slight_smile:

Wow, looks like I got here in just the nick of ti-
Seriously, though, good job on working through this!!

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms