- Home /
Hard question about saving and loading
Hello, Hope you are doing good. I have a system for creating a fortress in my game, I have 8 Gameobjects, and inside those 8 GameObjects, I have like 23 more GameObjects inside each one, each one of these objects has a script that contains a variable called "Cubo" (Cube in Spanish). I want to know a way of saving the value of "Cubo" in each one, and then, load them properly, but I cant figure out how, does anybody know how to do this?
Here is an image so you can see more crearly what is happening:
So, Here you can see the GameObjects "Fila1", "Fila2", etc go fro Up to Down, not from left to right.
So, anybody knows how yo do this?
Answer by Howey-Do-It · Apr 20, 2013 at 10:52 PM
For saving/loading you should look into Player Preferences, specifically, PlayerPrefs.SetString/GetString.
docs.unity3d.com/Documentation/ScriptReference/PlayerPrefs.html docs.unity3d.com/Documentation/ScriptReference/PlayerPrefs.SetString.html docs.unity3d.com/Documentation/ScriptReference/PlayerPrefs.GetString.html
Sorry, the hyperlink button isn't working on my browser.
I tested it and if I understand correctly this C# code should work:
using UnityEngine;
using System.Collections;
public class SaveLoad : MonoBehaviour {
public string[] loadedData;
public int convertBack;
// Update is called once per frame
void Update ()
{
// Easy way to test it press "S" in game to save and "L" to load
if(Input.GetKeyDown("s"))
SaveCubos();
if(Input.GetKeyDown("l"))
LoadCubos();
}
public void SaveCubos()
{
GameObject[] cubos = GameObject.FindGameObjectsWithTag("Cubos"); // If you create a new tag "Cubos" and tag all of the cubos with it
string saveData = "";
foreach(GameObject specificCubo in cubos)
{
EspacioCreacion cuboScript = specificCubo.GetComponent<EspacioCreacion>();
saveData = saveData + cuboScript.cubo.ToString() + ","; // The additional comma is for splitting the string later
}
Debug.Log(saveData); // To display what information we actually saved
PlayerPrefs.SetString("SaveData", saveData); // Actually save the information to PlayerPrefs
}
public void LoadCubos()
{
string loadData = PlayerPrefs.GetString("SaveData"); // Retrieve the information we saved
loadedData = loadData.Split(','); // Divides the information up based on the commas "," into a string array
// To convert the information back into integers or floats you could use:
convertBack = int.Parse(loadedData[0]);
}
}
I guess it matters whether you want to assign the data back to a specific cube. If so you will probably need to save more information that associates the "cubos" with each specific cube. You will also need to tag each object with the "Cubos" tag.
I hope that answers your question.
Thanks, God bless!
Howey
Its good but the problem is that this saves the info randomly, I need it in order so then I can load it in order, how can I do that?
Are the cubes static? Do they ever change in a way that would affect the saving process? Does the number of them ever shrink/increase? If any of these are true it is going to be more complex, if not it should be pretty simple.
I made it more simple, I erased all the "Fila1", "Fila2", "Fila3", etc... I also erased the "Espacio1", "Espacio2", "Espacio3", etc... I made one big Object with 160 GameObjects named from "1" to "160", that way is easier to sort them in a List, and to save them, I got what I wanted, thanks a lot my friend!
(However If you think there is a more optimized way to do this let me know please, the users are first!)
Alright, awesome! I just want to make sure you are set up properly. Did you figure out how to order them in the list properly?
[EDIT]: Sorry, I didn't read your comment very well. Unless you already figured this out, you could cycle through each of those 1-160 objects using an INT - STRING conversion and vice versa, adding 1 each time you check. But you probably already know that.
Have great one!
God bless!
Howey
Thats why I did exactly, here is the code so people can use it if they need it...
public List<GameObject> Cubos;
void Start() {
Cubos = new List<GameObject>();
GameObject[] go = GameObject.FindGameObjectsWithTag("Cubo");
foreach(GameObject Cubito in go) {
Cubos.Add(Cubito);
}
Cubos.Sort(
delegate(GameObject i1, GameObject i2)
{
return int.Parse(i1.name).CompareTo(int.Parse(i2.name));
}
);
}
And then I save and load them with the functions you wrote, thanks a lot, couldnt have done it without you! Have a nice day ;)
Your answer
Follow this Question
Related Questions
Getting error on instatiating. 0 Answers
How to save/load references to components and prefabs 1 Answer
Scene Saving 1 Answer