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;
}
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.
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.
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.
}