- Home /
[SOLVED] Save System with Serialize multiple objects
I'm trying to create a complex save system that, when you save, serializes every data of the game.
I saw this tutorial: https://www.youtube.com/watch?v=dRDIiK7AGn4&ab_channel=CSharpAccentTutorials and it helps me so much for saving player position.
Now i want to do something else: i want save all doors state, so if a door is open and i save, when i load the game that door will be open. Anyway in the map there are more doors, and i'm trying to save all of them.
Obviously i don't want create one var for every doors in the game, so i want call them with GameObject.FindGameObjectsWithTag ("name of tag"). But i can't create only one var because this one will be the same for every door.
So my question is: how can i create multiple serialized strings and how can i call them, linking these with each respective door?
You have to find a way to identfy each individual door. You could give each a unique ID, for example, ans use it to connect a door's state with the id, and after loading find a door and set it's state again.
after doing this how can i call them? using for cycle?
You could write a custom $$anonymous$$onoB. class that holds the ID and open state, and add it to each door. Then when saving, collect all instances of this component, and use a dictionary to connect the ID with the state. You could create an instance of another custom class that holds this dictionary, and serialize it. When loading / deserializing, go through each component in the scene again and if the ID from the component and the dictionary key match, assign the corresponding state.
Answer by bromley · May 26, 2016 at 08:03 AM
I solve it, look the new script:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using UnityEngine.SceneManagement;
public class SaveSystem : MonoBehaviour {
GameObject player;
GameObject cam;
GameObject start;
GameObject[] doors;
GameObject[] slideSDoors;
Start_Scene startScript;
private Scene scene;
private int doorsCount;
private int slideSDoorsCount;
private int totalDoorsCount;
void Start()
{
player = GameObject.Find ("PlayerController");
cam = GameObject.Find ("Camera");
start = GameObject.Find ("Start Scene");
doors = GameObject.FindGameObjectsWithTag ("Door");
slideSDoors = GameObject.FindGameObjectsWithTag ("SlideSecretDoor");
startScript = start.GetComponent<Start_Scene> ();
doorsCount = doors.Length;
slideSDoorsCount = slideSDoors.Length;
totalDoorsCount = doorsCount + slideSDoorsCount;
}
public void SaveData ()
{
BinaryFormatter bf = new BinaryFormatter ();
FileStream file = File.Create (Application.persistentDataPath + "/Data.dat");
PlayerData data = new PlayerData ();
data.doorState = new string[totalDoorsCount];
data.lockState = new string[totalDoorsCount];
for (int i = 0; i <= totalDoorsCount; i++)
{
foreach (GameObject door in doors)
{
Rotating_Normal_Door doorScript = door.GetComponent<Rotating_Normal_Door> ();
if (doorScript.doorID == i)
{
data.doorState [doorScript.doorID - 1] = doorScript.doorState;
data.lockState [doorScript.doorID - 1] = doorScript.lockState;
}
}
}
for (int i = 0; i <= totalDoorsCount; i++)
{
foreach (GameObject slideSDoor in slideSDoors)
{
Sliding_Secret_Door slideSecretScript = slideSDoor.GetComponent<Sliding_Secret_Door> ();
if (slideSecretScript.doorID == i)
{
data.doorState [slideSecretScript.doorID - 1] = slideSecretScript.doorState;
data.lockState [slideSecretScript.doorID - 1] = slideSecretScript.lockState;
}
}
}
scene = SceneManager.GetActiveScene ();
data.sceneName = scene.name;
data.posx = player.transform.position.x;
data.posy = player.transform.position.y;
data.posz = player.transform.position.z;
data.rotx = cam.transform.localRotation.x;
data.roty = cam.transform.localRotation.y;
data.rotz = cam.transform.localRotation.z;
data.rotw = cam.transform.localRotation.w;
bf.Serialize (file, data);
file.Close ();
}
public void LoadData()
{
if(File.Exists (Application.persistentDataPath + "/Data.dat"))
{
BinaryFormatter bf = new BinaryFormatter ();
FileStream file = File.Open (Application.persistentDataPath + "/Data.dat", FileMode.Open);
PlayerData data = (PlayerData)bf.Deserialize (file);
file.Close ();
PlayerPrefs.DeleteAll ();
SceneManager.LoadScene (data.sceneName, LoadSceneMode.Single);
Vector3 newPlayerPos = new Vector3 (data.posx, data.posy, data.posz);
Quaternion newPlayerRot = new Quaternion (0, data.roty, 0, data.rotw);
startScript.LoadDataState (newPlayerPos, newPlayerRot);
for (int i = 0; i <= totalDoorsCount; i++)
{
foreach (GameObject door in doors)
{
Rotating_Normal_Door doorScript = door.GetComponent<Rotating_Normal_Door> ();
if (doorScript.doorID == i)
{
doorScript.LoadData (data.doorState [doorScript.doorID - 1], data.lockState [doorScript.doorID - 1]);
}
}
}
for (int i = 0; i <= totalDoorsCount; i++)
{
foreach (GameObject slideSDoor in slideSDoors)
{
Sliding_Secret_Door slideSecretScript = slideSDoor.GetComponent<Sliding_Secret_Door> ();
if (slideSecretScript.doorID == i)
{
slideSecretScript.LoadData (data.doorState [slideSecretScript.doorID - 1], data.lockState [slideSecretScript.doorID - 1]);
}
}
}
}
}
}
[Serializable]
class PlayerData
{
public string sceneName;
public float posx;
public float posy;
public float posz;
public float rotx;
public float roty;
public float rotz;
public float rotw;
public string[] doorState;
public string[] lockState;
}
using the Arrays and adding different IDs for each door i solved my problem
thanks for support
Answer by Mmmpies · May 25, 2016 at 10:53 AM
Probably best to store them in an array if each door has a number you'd only need a boolean array to say if it's open or shut.
Save that array when you exit and load it when you startup and set the doors respectively.
this can be good using PlayerPrefs, but i want use an encrypted file where serialized values will be saved... i want to create a save system like "resident evil" where you need use a typewriter, so i think that i can't use your method
Your answer
Follow this Question
Related Questions
object list not serializing 2 Answers
SAVE OBJECTS IN ARRAY 1 Answer
Serialize large array of class 0 Answers
Moving multiple objects independenty on mobile using touchscreen 2 Answers
Delete a serialized binary save file 2 Answers