Page 1 of 1

Saving component data

Posted: Sat Jun 24, 2023 2:29 am
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.

Re: Saving component data

Posted: Sat Jun 24, 2023 10:37 am
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.

Re: Saving component data

Posted: Sun Jul 02, 2023 5:24 am
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;
            }
        }

    }

}

Re: Saving component data

Posted: Sun Jul 02, 2023 7:41 am
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.