Saving component data

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
McSwan
Posts: 7
Joined: Sat Jun 24, 2023 2:21 am

Saving component data

Post by McSwan »

Hi,

I was wondering if there was a "save component state" in the savers. I can save position etc. but during quests I give a npc a point transform to walk to, and this is not saved. [it's placed in a public component
eg public transform destination]
Do I need to write a custom script for this? I have the easy save system, so I'm guessing I could integrate this in as well.

or I could disable saving during tis time.
User avatar
Tony Li
Posts: 21681
Joined: Thu Jul 18, 2013 1:27 pm

Re: Saving component data

Post by Tony Li »

Hi,

Write a custom saver: How To: Write Custom Savers

They're pretty simple to write. This way you can save exactly what you want to save, without saving extraneous data like a "save component" script would.

Also, unlike just saving data like Easy Save does, with a custom saver you can actually resume processes such as coroutines or NavMeshAgent navigation in your ApplyData() method.

That said, Easy Save does a great job of storing and retrieving data to permanent storage such as disk files. The Dialogue System has Easy Save integration.
McSwan
Posts: 7
Joined: Sat Jun 24, 2023 2:21 am

Re: Saving component data

Post by McSwan »

Thank-you so much!

Code: Select all

using Pathfinding;
using System;
using UnityEngine;

// C: \Unity\Fulfillment\Assets\Plugins\Pixel Crushers\Common\Templates\SaverTemplate.cs

namespace PixelCrushers
{

    public class SaveAiDestination : Saver // Rename this class.
    {

        Transform target;

        public override string RecordData()
        {
            target = GetComponent<AIDestinationSetter>().target;
            if (target)
            {
                return GetComponent<AIDestinationSetter>().target.name;
            }
            return "";
        }

        public override void ApplyData(string s)
        {
            GameObject target = null;
            if (!String.IsNullOrEmpty(s))
            { 
                target = GameObject.Find(s);
            }
            if (target)
            {
                GetComponent<AIDestinationSetter>().target = target.transform;
            }
        }

    }

}
User avatar
Tony Li
Posts: 21681
Joined: Thu Jul 18, 2013 1:27 pm

Re: Saving component data

Post by Tony Li »

Looks good! If you need to call a method or start a coroutine to resume pathfinding, you can also do that in the ApplyData() method.
Post Reply