Page 1 of 1

Saving a bool state in a custom script [Solved]

Posted: Mon Sep 13, 2021 6:40 am
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

Re: Saving a bool state in a custom script

Posted: Mon Sep 13, 2021 8:32 am
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];
        }
    }
}

Re: Saving a bool state in a custom script

Posted: Tue Sep 14, 2021 11:12 pm
by nathanj
Awesome,

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

Nathan

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

Posted: Wed Sep 15, 2021 7:52 am
by Tony Li
Happy to help!