container class save based on Cherno's example

@Cherno

Howdy,

To create a serialize save-load system to store global variables and gameobjects (as in only the data that I need to save of the gameobjects) I followed one of Cherno’s answers (atleast I remember it was from Cherno). Sadly I can’t find the right link anymore.

But anyway from what I understand the example uses a container class for each gameobject and adds a list of which point to those containers classes in an other other main class which also stores global variables. This main class is serialized / saved.

The problem is that I still have some bug with linking the container classes and the main class. So it now only saves the global variables but not yet the gameobjects (the problem lies with the saving part since no matter how much gameobjects I add, the safe file remains tiny). Here is how main class and the container classes look like atm:

[Serializable]
public class SaveData { //stores all scene objects + global vars
    public List<SaveData_SceneObject> sceneObjects = new List<SaveData_SceneObject>(); 

	//Global vars
	public int testNum = 0;
}

[Serializable]
public class SaveData_SceneObject { //container class
	public string name;
	public int id;
	public bool dontLoad;
	public float posX;
	public float posY;
	public float posZ;
	public float rotX;
	public float rotY;
	public float rotZ;
	public float rotW;
	public float scaleX;
	public float scaleY;
	public float scaleZ;
}

Now to the save part (I think I am doing something wrong here or in the classes above):

SaveData _data = new SaveData (); //class with global vars and lists of scene objects
StoreSceneObjects (); //Store scene objects in classes (see below for details)
SaveLoad.SaveToFile(_data, _savename); //this calls a function that serializes the data, but the serialize part is fine it is the _data that is wrong/lacking I think

And finally a StoreSceneObjects (); (please note the I add a debug.log at the bottom which shows that this function does run properly, also note that sceneObjectID is just a c# script that gives each object an ID number):

public void StoreSceneObjects () {
		object[] obj = GameObject.FindObjectsOfType(typeof (GameObject));
		foreach (object o in obj) {
			GameObject g = (GameObject) o;
			sceneObjectID _idScript = g.GetComponent<sceneObjectID>();
			if (_idScript != null) {
				SaveData_SceneObject _dataObject = new SaveData_SceneObject();
				_dataObject.name = g.name;
				_dataObject.id = _idScript.id;
				_dataObject.dontLoad = _idScript.dontLoad;

				_dataObject.posX = g.transform.position.x;
				_dataObject.posY = g.transform.position.y;
				_dataObject.posZ = g.transform.position.z;

				_dataObject.rotX = g.transform.rotation.x;
				_dataObject.rotY = g.transform.rotation.y;
				_dataObject.rotZ = g.transform.rotation.z;
				_dataObject.rotW = g.transform.rotation.w;

				_dataObject.scaleX = g.transform.localScale.x;
				_dataObject.scaleY = g.transform.localScale.y;
				_dataObject.scaleZ = g.transform.localScale.z;

				Debug.Log("GameObject saved: " + _dataObject.name);
			}
		}
	}

Thanks for taking the time!

Uhm, in line 7 of your last code fragment you create a new instance of your “SaveData_SceneObject” class, you fill in some information, but you don’t do anything with that instance. I guess you want it to add to the “sceneObjects” list inside your “SaveData” class.

For this your “StoreSceneObjects” method should have a parameter of type “SaveData” so you can add the instances to the list

public void StoreSceneObjects (SaveData data) {
    object[] obj = GameObject.FindObjectsOfType(typeof (GameObject));
    foreach (GameObject g in obj) {
        sceneObjectID _idScript = g.GetComponent<sceneObjectID>();
        if (_idScript != null) {
            SaveData_SceneObject _dataObject = new SaveData_SceneObject();
            // [ ... ]
            data.sceneObjects.Add(_dataObject);
        }
    }
}

So obviously you have to call your method like this:

SaveData _data = new SaveData ();
StoreSceneObjects (_data);
// [...]

If you take a look at your code, since _data is a local variable, how should “StoreSceneObjects” be able to add / modify anything inside your _data class?