Before I start, I’m pretty sure this is not an XML serialization issue. If it is, definitely let me know!
Let’s say I have an XML structure similar to the following:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<animals>
<section name="Weird Animal Group 1">
<animal qty="3">Three-Legged Cow</card>
<animal qty="3">Tim the Horse</card>
<animal qty="2">Dog</card>
<animal qty="3">Puppy</card>
<animal qty="3">Cat</card>
<animal qty="3">Kitty</card>
<animal qty="2">Monkey 2.0</card>
<animal qty="2">Robot Dinosaur</card>
...
<animal qty="1">Pen_guin</card>
<animal qty="2">h-y-p-h-e-n-a-t-e-d r-a-b-b-i-t</card>
</section>
</animals>
Let’s also say (as I’ve tried to illustrate with the elipsis) that there’s some huge amount of these animal nodes (hundreds).
In my scene, I have an individual prefab for every type of animal that could come in through this XML file. What I need to be able to do is, at runtime, instantiate as many GOs of a particular type as specified by the “qty” attribute in the XML.
I’ve given every animal in my example strange and varied names using all sorts of weird string structures to illustrate that in my real game there is a comparable amount of unpredictable variety to the way things are named. The only thing I can count on is that they are consistent – i.e. “Monkey 2.0” will always be called “Monkey 2.0”.
Is there some way in C# to have some sort of object pool or database of objects that are mapped to strings like these? I’ve looked into dictionaries and hashtables – I think a dictionary might be the way to go – but I’m worried about efficiency. As I’ve said, there will be hundreds of these animal types.
I think setting up a structure like this (my full animal database, before reading XML) might be ok:
Dictionary<string, GameObject> dictionary = new Dictionary<string, GameObject>();
dictionary.Add("Three-Legged Cow", ThreeLeggedCow);
dictionary.Add("Tim the Horse", TimTheHorse);
dictionary.Add("Dog", Dog);
dictionary.Add("Puppy", Puppy);
...
dictionary.Add("Pen_guin", Penguin);
dictionary.Add("h-y-p-h-e-n-a-t-e-d r-a-b-b-i-t", HyRabbit);
and then, as I loop through and parse each node:
for (int i=0; i<qty; i++) {
GameObject currentAnimal = animals[animalNameAsStringFromXml];
GameObject.Instantiate(currentAnimal);
}
I’m just worried that using a dictionary in this manner might not be very efficient, especially considering there will be hundreds of these. Any ideas?
Thanks so much in advance for any suggestions. Any help is greatly appreciated