- Home /
How can I save a series of instantiated objects from an array?
Hi, I'm creating a slider game for mobile. My problem is that when the player makes progress and moves the squares around I want to save the position of each square , the puzzle instantiates when the scene starts (its on the start function, what it does it creates a 3x3 and divides the image in quads, so each quad is a piece of the image). I don't really understand how to save this type of objects, I've managed to learn how to save the points,the level and the time, but not the position of the objects. The way I can imagine it could be done is by traveling through the array then store each position every time an object is moved but I dont know how to do it. They are Vector2Int if you need more information I can provide.
Thanks in advance :)
Answer by qobion · Mar 24, 2019 at 12:15 PM
If you want to save vectors or arrays in playerprefs you can convert it to string and save this string with some formatting so then you can convert it again to array back. This is look like this:
Vector2Int [] positions;
void SavePositions()
{
string s = "";
foreach (Vector2Int c in positions)
{
s += string.Format("{0}:{1}:",c.x,c.y);
}
PlayerPrefs.SetString("MyPositions", s);
}
void LoadPositions()
{
string s = PlayerPrefs.GetString("MyPositions");
if (s == null)
return;
string[] p = s.Split(':');
for (int i = 0; i < p.Length; i+=2)
{
positions[i].x = System.Convert.ToInt32(p[i]);
positions[i].y = System.Convert.ToInt32(p[i+1]);
}
}
Answer by saR · Mar 24, 2019 at 01:30 PM
Thank you for your answer @qobion ! I think I understood the code you've written. So what I should do is save each position by having a public static Vector2Int on the blocks script so I save each position and then replace it in the code you just said.
The thing is that I dont really understand where to replace it , I'm so sorry , I just finished my first year with c# and in the code you just said there's some syntaxis I dont understand, but anyways , thank you for your response I really apreciate it !
This is how you can save Array of Vector2Int.
If you want to save single Vector2
void SavePosition()
{
string s = string.Format("{0}:{1}", transform.position.x, transform.position.y);
PlayerPrefs.SetString(gameObject.name, s);
}
void LoadPosition()
{
string s = PlayerPrefs.GetString(gameObject.name);
if (s == null)
return;
string[] p = s.Split(':');
Vector2 position = new Vector2(float.Parse(p[1]), float.Parse(p[2]));
}
$$anonymous$$y problem now is that is saving much more than I need , here's my saving script:
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.A))
{
listaVectores.Clear();
string s = "";
Puzzle._bloqueVacio.gameObject.SetActive(true);
int contador = 0;
int[,] testeo = new int[Puzzle.tamañoPuzzle,Puzzle.tamañoPuzzle];
for (int h = 0; h < Puzzle.tamañoPuzzle; h++)
{
for (int i = 0; i < Puzzle.tamañoPuzzle; i++)
{
contador++;
int b = GameObject.Find("Quad" + contador).GetComponent<Bloque>().coordenada.x;
int a = GameObject.Find("Quad" + contador).GetComponent<Bloque>().coordenada.y;
testeo[i, h] = b;
print("Posiciones x:" + b);
print("Posiciones y:" + a);
listaVectores.Add(GameObject.Find("Quad" + contador).transform.position);
s += string.Format("{0}:{0}", b, a);
PlayerPrefs.SetString("PosicionsBloques", s);
}
}
SaveGame.Save("posicionesVectores.dat", listaVectores);
print("s length: "+s.Length);
print("Playerprefs posicions bloques: " +PlayerPrefs.GetString("PosicionsBloques"));
}
I dont know why it's saving only positions 0 1 2 and not saving 3 4 5 , 6 7 8 , Puzzle._tamañoPuzzle is 9 in this case. I Debug.Log the s.length and it end's up at 27 , also the coordinates are wrong , the vector3 are not but the int ones of a and b are wrong , I cant find the error :( .
Your answer
Follow this Question
Related Questions
Save/Load Animation State of Instantiated Prefabs 0 Answers
Best Way to Store Large Number of GameObjects? 1 Answer
How to save a game 1 Answer
Serialization 2 Answers
Serializing PlayerPrefs 1 Answer