Save Bool Data

Hi,

In lecture 85 of the new RPG course, Sam’s states that the save system can be used to save a bool; however, he doesn’t discuss how. If I try to just to enter a bool into the Capture Method, it returns an error. In my game, I have a bool called miniFigMojo that I would like saved and recalled. How would I go about including this in the saved game data?

Here’s the current save game code:

        public object CaptureState()
        {
            return energyPoints.value;
            //return miniFigMojo;
        }

        public void RestoreState(object state)
        {
            energyPoints.value = (float)state;
        }

Thanks.

You might want to peruse the next section in the course which goes into the creation of the saving system and it may make a bit more sense on how to achieve this.

I’ve completed that section. All of Sam’s examples involve saving float/int data. Does anyone have an example of saving Bools?

I’m not sure if you can save a bool, but if you already know how to save a float/int why not just do that.

Have an int/float called isPlayerDead for example and have 0 = false and 1 = true, or something like that.

@sampattuzzi I saw a discussion on this in the Q&A but now cannot find it.
Any insights on this?

Yes, you can save a bool. Here’s an example how:

    public object CaptureState()
    {
        return isEmpty;
    }

    public void RestoreState(object state)
    {
        isEmpty = (bool)state;

        if (isEmpty == true)
        {
            gameObject.SetActive(false);
        }
    }

So, isEmpty is the bool variable for a item chest. The restore checks if the chest is empty and, if so, disables it.

1 Like

Yes, saving bools should be pretty easy. I notice that in your original example you were casting to float. Was that the issue you had?

Hi Sam,
Thank you for your response. It turns out my real problem was that I wanted to capture more than one type of data (a bool and a float). I found and watched the bonus lecture on the topic (I missed it on my first go through the course) but I wasn’t able to replicate it (I tried both methods mentioned in the lecture). If you have any suggestions on how I might be able to do that, I’d greatly appreciate it. :smiley:

Hi, Mike. Here’s an example of how i used the save system to capture multiple data types. I’ll paste the code, and explain what’s going on as best I can:

    //first, create a serializable class that will be used to capture multiple values.
    //this class is contained within the class that has the variables I want to save.
    //for this example, I want to save the variables: XPTowardsNextPoint, CurrentPoints, and IsAlive.

    //this class lives inside CharXP class (a MonoBehaviour that implements ISaveable)
    [System.Serializable]
    class MySaveData
    {
        //create variables for each field I want to save
        public int XP_TowardsNextPoint;
        public int currentNumOfPoints;
        public bool charIsAlive;
    }

    //CaptureState() is part of the save system Sam built (see ISaveable)
    public object CaptureState()
    {
        //create an instance of the MySaveData class
        var saveData = new MySaveData();

        //we're going to record the current values of the variables in this CharXP instance 
        saveData.XP_TowardsNextPoint = XPTowardsNextPoint;
        saveData.currentNumOfPoints = CurrentPoints;
        saveData.charIsAlive = IsAlive;

        //the object we return here is of type MySaveData and has the values we want saved
        return saveData;
    }

    //RestoreState() is part of the save system Sam built (see ISaveable)
    public void RestoreState(object state)
    {
        //create a variable of type MySaveData called saveData; cast the parameter, "state",
        //as a MySaveData and assign it to saveData.
        var saveData = (MySaveData)state;

        //set the values of this instance of CharXP == the values that we saved.
        CurrentPoints = saveData.currentNumOfPoints;
        XPTowardsNextPoint = saveData.XP_TowardsNextPoint;
        IsAlive = saveData.charIsAlive;
        //Note: the details of how the data is written to / read from a file on your computer
        // when CaptureState() / RestoreState() are called is part of the save system itself,
        //but this should give you a good idea of how to use it to do what you are trying to do.
    }
2 Likes

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms