So I've done a bit of saving of individual objects and saving a position array of multiple objects, but now running into something that's stumping me.
Here's what I want to do. I want to save the state of 3 instances of a prefab which I've made.
For the purposes of saving, the only variables I adjust are their Transform.positions and 2 floats on a component script (Rope Length and Rope Thickness).
How do I save these items taking into account each of their positions and 2 float values, and then load them up again?
This seems like a fairly lightweight example compared to say an RPG where I'd need to save several values per character (I'll get there, but baby steps!)
Cheers,
Jesse
Saving and Loading multiple objects with parameters
Re: Saving and Loading multiple objects with parameters
Hi Jesse,
There are a number of ways you can approach this, but the way I find best is to append a number to the end of a tag. So the first prefab could have the tag prefab0, the second prefab1, and so on. If the prefabs are in a List or an array, you can then just iterate through and load each one separately. For example:
And then to load:
(I've typed this directly into the forum, so apologies for any small errors.)
If the prefabs are instantiated at runtime, you could also save the length of the prefabList when saving. Then inside the loop when loading you could instantiate a prefab, put it into the prefab list and then load into that, like this example.
All the best,
Joel
There are a number of ways you can approach this, but the way I find best is to append a number to the end of a tag. So the first prefab could have the tag prefab0, the second prefab1, and so on. If the prefabs are in a List or an array, you can then just iterate through and load each one separately. For example:
Code: Select all
// Let's assume this list is already filled with prefabs
List<GameObject> prefabList;
// Iterate over our prefab list and save each one.
for(int i=0; i<prefabList.Count; i++)
{
// Save our position and append the number of the prefab to the end of the tag
ES2.Save(prefabList[i].transform.position, "myFile.txt?tag=prefab"+i);
// Get our Rope component
Rope rope = prefabList.GetComponent<Rope>();
// Save our rope length and thickness, again appending the prefab number
ES2.Save(rope.ropeLength, "myFile.txt?tag=ropeLength"+i);
ES2.Save(rope.ropeThickness, "myFile.txt?tag=ropeThickness"+i);
}
Code: Select all
// Again, let's assume this is filled with our prefabs.
List<GameObject> prefabList;
// If there's no data to load, do nothing.
if(!ES2.Exists("myFile.txt")
return;
// Iterate over our prefab list and load each one.
for(int i=0; i<prefabList.Count; i++)
{
// Save our position and append the number of the prefab to the end of the tag
prefabList[i].transform.position = ES2.Load<Vector3>("myFile.txt?tag=prefab"+i);
// Get our Rope component
Rope rope = prefabList.GetComponent<Rope>();
// Load our rope length and thickness
rope.ropeLength = ES2.Load<float>("myFile.txt?tag=ropeLength"+i);
rope.ropeThickness = ES2.Load<float>("myFile.txt?tag=ropeThickness"+i);
}
If the prefabs are instantiated at runtime, you could also save the length of the prefabList when saving. Then inside the loop when loading you could instantiate a prefab, put it into the prefab list and then load into that, like this example.
All the best,
Joel