Saving a bool state in a custom script [Solved]

Announcements, support questions, and discussion for Save System for Opsive Character Controllers.
Post Reply
User avatar
nathanj
Posts: 303
Joined: Sat May 28, 2016 12:30 am

Saving a bool state in a custom script [Solved]

Post by nathanj »

Hi Again ;)

What would be the best (or preferably easiest)way to save the state of a custom script. for example, I have a generic custom crafting script that enables and disables game objects if conditions are met (first is checks the local bool (isStageComplete), if it's false it then checks the players inventory (UIS) if they have the appropriate items in their inventory, if they do then it runs a Unity event to enable and disable assigned game objects and it then sets isStageComplete to true. The next time the player interacts with this object it moves on to the next.

Can you think of a way to save the isStageComplete value with the save system? I am able to save all of the game object states with the MultiActiveSaver component but I need to be able to save the state of the script itsself. I was thinking I could run a check once the game objects states are set to then determine what state the crafting scipt is at and set it then, but I feel like there might be a better way to achieve this.

Here's the code incase the above is a bit confusing.

Code: Select all

 public void Interact(GameObject character)
    {
        int arrayCount = 0;
        int arrayLength = constructorClass.Length;
        Inventory playerInv = character.GetComponent<Inventory>();
        foreach (VSBuildableClass constructor in constructorClass)
        {
            arrayCount = arrayCount + 1;
            if (constructor.stageCompleted == false)
        {
            //if no item is required in the player's inventory 
            if (constructor.itemDefinition == null)
            {
                constructor.stageCompleted = true;
                constructor._event.Invoke();
                break;
            }

            if (playerInv.GetItemAmount(constructor.itemDefinition) >= constructor.amountRequired)
            {
                playerInv.RemoveItem(constructor.itemDefinition, constructor.amountRequired);
                constructor._event.Invoke();
                constructor.stageCompleted = true;
            }
            else
            {
                int amountNeeded = constructor.amountRequired - playerInv.GetItemAmount(constructor.itemDefinition);
                string message = "You need to gather" + amountNeeded + " " + constructor.itemDefinition.name.ToString();
                DialogueManager.ShowAlert(message);
            }
            break;
        }
        }                     
    }
and the consurtuctor class:

Code: Select all

public class VSBuildableClass
{
    public ItemDefinition itemDefinition;
    public int amountRequired;
    public bool stageCompleted = false;
    public UnityEvent _event;
}

I hope this isn't too convoluted. Thank you.
Nathan
Last edited by nathanj on Tue Sep 14, 2021 11:12 pm, edited 1 time in total.
User avatar
Tony Li
Posts: 22098
Joined: Thu Jul 18, 2013 1:27 pm

Re: Saving a bool state in a custom script

Post by Tony Li »

Hi Nathan,

You can write a custom Saver class. It might look roughly like this, which assumes ConstructorSaver has access to your constructorClass data so you'll probably want to put it on the same GameObject that has that data:

Code: Select all

public class ConstructorSaver : Saver
{

    [System.Serializable]
    public class Data
    {
        public bool[] stageCompleted;
    }

    public override string RecordData()
    {
        var data = new Data();
        data.stageCompleted = new bool[constructorClass.Length];
        for (int i = 0; i < constructorClass.Length; i++)
        {
            data.stageCompleted[i] = constructorClass[i].stageCompleted;
        }
        return SaveSystem.Serialize(data);
    }

    public override void ApplyData(string s)
    {
        if (string.IsNullOrEmpty(s)) return;
        var data = SaveSystem.Deserialize<Data>(s);
        if (data == null) return;
        for (int i = 0; i < constructorClass.Length; i++)
        {
            constructorClass[i].stageCompleted = data.stageCompleted[i];
        }
    }
}
User avatar
nathanj
Posts: 303
Joined: Sat May 28, 2016 12:30 am

Re: Saving a bool state in a custom script

Post by nathanj »

Awesome,

Thanks again Tony for giving such a good example. This is extremely helpful for all my other custom saving needs.

Nathan
User avatar
Tony Li
Posts: 22098
Joined: Thu Jul 18, 2013 1:27 pm

Re: Saving a bool state in a custom script [Solved]

Post by Tony Li »

Happy to help!
Post Reply