Alternative Approach

When I watched this lesson, I thought it was a little bit silly to make an extra internal class just for saving a few bits of data (not that this is a BAD approach, but just to me, for 2 variables, it seems excessive).

So, instead I simply made a new List of strings, and added my QuestID (or QuestName, if that’s what you prefer) as the first element in the list. Then when you restore it, you simply use that first element to get the quest, and use the rest to fill in your objectives list. Easy!

        public object CaptureState()
        {
            List<string> state = new List<string>();

            state.Add(quest.GetQuestID());

            foreach(string completedObjective in completedObjectives)
            {
                state.Add(completedObjective);
            }

            return state;
        }

        public QuestStatus(object objState)
        {
            List<string> restoredState = objState as List<string>;
            completedObjectives.Clear();

            //Get the Quest
            quest = Quest.FindQuestByID(restoredState[0]);
            restoredState.Remove(restoredState[0]);

            //Get the Completed objectives - if there are any
            if(restoredState.Count > 0)
            {
                foreach(string restoreObjective in restoredState)
                {
                    completedObjectives.Add(restoreObjective);
                }
            }
        }
1 Like

For reference, I’m using a Guiid to make the QuestID

[SerializeField] string questID = System.Guid.NewGuid().ToString();

(serialize so that it makes the ID right away in the inspector and then saves that ID on the quest object, or you can just manually create one)

A good solution, especially popping the quest id off before restoring the objectives. Well done!

Privacy & Terms