- Home /
Saving and Loading Data with a for loop
HI,
I have a question about loading your data. I've managed to extract a random ID number from gameObjects and store them in a file:
for (int i = 0; i < characterIDList.Count; i++) {
data.UniqueID = characterIDList;
Debug.Log(data.UniqueID[0]);
Debug.Log(data.UniqueID[1]);
}
but when loading I have a problem. It needs to get the list of gameobjects from the List characters and apply the stored data from the data.List UniqueID onto them. This is the code I have in the load function:
for(int y = 0; y < characters.Count; y++){
for(int i = 0; i < data.UniqueID.Count; i++)
{
characters[y].GetComponent<EntityScript>().characterID = data.UniqueID[i];
}
}
It changes all the charactersID's to the same value. How do i do it so that they all get their correct ID's back?
Please explain what this piece of code is supposed to do, specifically why you iterate through the list but assign the whole list to data.UniqueID with each iteration.
for (int i = 0; i < characterIDList.Count; i++) {
data.UniqueID = characterIDList;
Debug.Log(data.UniqueID[0]);
Debug.Log(data.UniqueID[1]);
}
Every GameObject with the EntityScript attached will give his personal ID to the characterIDList. When I exit the scene or click on the save button I want to copy all the strings from the characterIDList to the data.UniqueID string list.
Cherno is right. You are making an iteration but never use it. It should be something more like:
for (int i = 0; i < characterIDList.Count; i++) {
data.UniqueID[i] = characterIDList[i];
}
But seeing only that part of the code it's hard to tell you how exactly it should be.
Here is the full code. Some of it might not work since I didn't really test it yet. Every single Gameobject with EntityScript add himself to the characters and characterIDList.:`
using UnityEngine; using System.Collections; using System; using System.Runtime.Serialization.Formatters.Binary; using System.IO; using System.Collections.Generic;
[Serializable] public class GameSettings : $$anonymous$$onoBehaviour {
public static GameSettings control;
public GameObject player; //for finding the player gameobject
public Vector3 playerPosition; //for getting the players position
public static List<GameObject> characters;// = new List<GameObject> (); //getting all the characters
public static List<string> characterIDList;// = new List<string>(); //for setting the character ID
//creating a list of letters and numbers for a unique ID list
private static string[] idIdentifier = new string[] {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k",
"l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v",
"w", "x", "y", "z", "0", "1", "2", "3", "4", "5", "6",
"7", "8", "9"};
//creating an Array with all possible ID's
private static ArrayList uniqueIDList = new ArrayList();
void Start(){
characters = new List<GameObject> ();
characterIDList = new List<string> ();
}
void Update(){
if (control == null) {
DontDestroyOnLoad (this);
control = this;
}
else if (control != this) {
Destroy(gameObject);
}
}
public void SaveData(){
BinaryFormatter bf = new BinaryFormatter ();
FileStream file = File.Create (Application.persistentDataPath + "/playerinfo.dat");
PlayerData data = new PlayerData ();
for (int i = 0; i < characterIDList.Count; i++) {
data.UniqueID = characterIDList;
Debug.Log(data.UniqueID[0]);
Debug.Log(data.UniqueID.Count);
Debug.Log(characterIDList.Count);
}
bf.Serialize (file, data);
file.Close ();
}
public void LoadData(){
if (File.Exists (Application.persistentDataPath + "/playerinfo.dat")) {
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Open(Application.persistentDataPath + "/playerinfo.dat", File$$anonymous$$ode.Open);
PlayerData data = (PlayerData)bf.Deserialize(file);
characterIDList = data.UniqueID;
for(int i = 0; i < characters.Count; i++){
characters[i].GetComponent<EntityScript>().characterID = characterIDList[i];
}
file.Close();
}
}
public static string getID() {
string uniqueID;
do{
uniqueID = "";
for(int i = 0; i < 8; i++) {
uniqueID += idIdentifier[UnityEngine.Random.Range(0, idIdentifier.Length)];
}
} while(uniqueIDList.Contains(uniqueID));
uniqueIDList.Add(uniqueID);
return uniqueID;
}
}
[Serializable] class PlayerData{ public List UniqueID = new List(); public int AmountOfCharacters; }
Take a look at barbe63's code example and try to understand what it does.
Answer by barbe63 · Jul 06, 2015 at 09:57 PM
Well I would say you have many things to do:
Assure that characterList is not empty. I assume it's not even if you don't posted the code for how you fill it.
make the UniqueID a string list so it would match the characterList type.
set the UniqueId list outside the loop. A single command would work.
data.UniqueID = new List(characterIDList); // copy
or
data.UniqueID = characterIDList; // reference
you can still use a for loop for debugging purpose only like this:
for (int i = 0; i < characterIDList.Count; i++) { Debug.Log(characterIDList[i]); } //or the same with data.UniqueID like this for (int i = 0; i < data.UniqueID.Count; i++) { Debug.Log(data.UniqueID[i]); }
Your Loading function seems better than the one you posted with the y. Which I understand now why it would always give the same value to all your characters. (giving the last value of the i iteration)
Answer by Voxel-Busters · Aug 08, 2015 at 06:32 PM
Simpler solution would be to directly serialize MonoBehaviour component, which internally saves all the essential properties. But MonoBehaviour serialization wont be possible using Binary Formatter.
But its possible using Runtime Serialization for Unity. Its not just another serialization plugin which works only on custom c# objects. But what makes it special is its capablity to serialize Unity Objects like GameObject, MonoBehaviours, Textures, Prefabs etc. As a matter of fact, you can even use it for Scene Serialization. For more info about supported list, please check this link.
Your answer
Follow this Question
Related Questions
Save/Load Objects settings(position, rotation ...) 1 Answer
Hard question about saving and loading 1 Answer
Saving and Loading and assinging to gameobject 0 Answers
Serialize GameObject 1 Answer
Save Multiple GameObject with XML 1 Answer