With apologies, my Google-fu has failed me. I’ve found dozens of topics on collectibles and not one has been able to address my actual question.
What is the ideal method for saving a “Collectible” game object’s state in a save-file?
What I’m trying to do right now is modify the “RollerBall” tutorial with a pause menu that allows you to save/load the level, preserving the player’s position, score (“count”) and the state of the various collectible cubes. I managed to get the pause menu in place and have code skeletons in place for saving & loading, but I have no idea how to go about preserving the Active/Inactive state of each specific collectible object.
Since I’m using this as a primer and test-bed to master more complex concepts down the road, I’m really looking for a method or structure that could handle multiple different types of collectibles (say a player picks up two objects worth 100pts and a third worth 500pts. Score-save is easy, knowing which collectibles were picked up and disabling them on reload is my stumbling block).
To be clear, I’m using serialization and have no intention of calling PlayerPrefs if I can help it.
EDIT:
So this is the code for my Save/Load class, which gets called from “MenuEsc” by method. Works beautifully for saving player position, but I’m still having a miserable time trying to wrap my brain around the problem of saving collectibles’ “picked-up” status.
using UnityEngine;
using System;
using System.Collections;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
public class MenuSaveLoad : MonoBehaviour
{
public static void SaveGame()
{
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Create(Application.persistentDataPath + "/savegame.sav");
LevelData data = new LevelData();
//Write game data into save structure here
data.PositionX = PlayerController.Instance.PlayerPosition.x;
data.PositionY = PlayerController.Instance.PlayerPosition.y;
data.PositionZ = PlayerController.Instance.PlayerPosition.z;
bf.Serialize(file, data);
file.Close();
print("Saved");
}
public static void LoadGame()
{
if (File.Exists(Application.persistentDataPath + "/savegame.sav"))
{
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Open(Application.persistentDataPath + "/savegame.sav",FileMode.Open);
LevelData data = (LevelData)bf.Deserialize(file);
file.Close();
//Sets player position straight from load data.
//Setting via Vector3 variable is preferable, but Roll-a-ball code handles player movement oddly.
GameObject.Find("Player").transform.position = new Vector3(data.PositionX,data.PositionY,data.PositionZ);
print("Loaded");
}
}
}
[Serializable]
class LevelData
{
//store "public" variables for saving & loading
public float PositionX, PositionY, PositionZ;
}
The tutorial code is kind of a pain this way, since it lumps player movements, interactions, and the “count” all into the PlayerController class. The only script on the collectibles themselves is the Rotator that makes them spin.
-I’m not adverse to moving things around if needed, just saying “this is my situation, please be clear about what I should add and where.”