Basically, I have a script on an object that spawns child objects on it. It adds them to this class:
public class TileItems
{
public List<Transform> ChildObjectTransforms;
public List<Vector3> ChildObjectPositions;
}
And to save them, I use JSON to save the class. This approach works fine, but I’ve run into an issue: I have multiple parent objects that each have different child objects spawned on them.
Is it possible to save multiple instances of the same class in JSON, then detect which class instance to load based on what the parent object is?
If I haven’t explained something enough, please let me know.
If I understand what you are asking then you could create a reader class. eg
string instance = "thisField";
class GetInstance
{
public string instance = null;
}
if (JsonUtility.FromJson<GetInstance>(File.ReadAllText(filePath)).instance == instance)
{
// call Json utility again with correct class
}
1 issue I ran into is that since my tiles are procedurally generated by spawning prefabs, each prefab has the same ID returned when using GetInstanceID();
Therefore, I needed to make my own InstanceID generator:
string GenerateRandomID()
{
//generate a very random string so each saved tile has a different ID. The more random it is, the better!
System.Random random = new System.Random();
const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz`~!@#$%^&*()-_=+[{]};:'/|.";
return new string(Enumerable.Repeat(chars, 15)
.Select(s => s[random.Next(s.Length)]).ToArray());
}
I was a little rushed in my initial answer and just wanted to introduce the idea that you can create a very small reader class to identify objects in JSON by any single node.
Is there any reason why you cant just save the tiles with the tile objects in their list? I used the node name “instance” purely to relate to your initial request. I didn’t actually mean for it to be a unique instance id.
I have had other solutions for lists etc of unknown identity. A converse method to the simply reader is a universal one. You could create a reader that has every possible node of all the tile items. After populating this object from the JSON file you could identify which class it should belong to by which nodes have been populated.
It is important to note that while reading JSON can be done with any reader, writing will only write the nodes present in the class. So every node in a writer class will be writen regardless of its value or even if it has been initialized.
The reason why I needed some ID system is because many objects can be saving the same class name (TileItems). So my issue was, how would 10 different objects be able to load 10 different class values, but the classes have the same name?
The JSON save file doesn’t even have a class name in it:
JSON save {“InstanceID”:“_v6#Wt1vlnt!Dj7”,“ChildObjectTransforms”:[{“instanceID”:-501160},{“instanceID”:-501182},{“instanceID”:-501204}],“ChildObjectPositions”:[{“x”:13.693446159362793,“y”:0.0,“z”:22.27127456665039},{“x”:7.535343170166016,“y”:0.0,“z”:-8.409782409667969},{“x”:19.39572525024414,“y”:0.0,“z”:-12.424758911132813}]}
So I was concerned that without an ID system, the objects wouldn’t be able to tell what child positions should be spawned.