Runtime instantiation based on XML

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 :slight_smile:

Have you tried this method, is it has any performance issues? Generate a bunch of data and test it out, to make sure. I think this method makes your code bloated, but performance-wise it doesn’t look bad.

You could run the parsing/instantiation in a new thread, so you don’t block your main thread while parsing in all those stuff, then it should be seamless.

Also take a look at this: Loading Resources. If you can set up a naming convention for the XML and parsing it with a suitable regex pattern, you could get the actual name of your prefabs from those strings in XML. I mean you would remove whitespaces and special characters, and make the words’ first characters capitalized. So with using a regex parser, “Three Legged Cow”, “Three-Legged Cow”, or “threeLegged Cow” would all mean “ThreeLeggedCow”, which is the name of your resource. Then you do:

Instantiate(Resources.Load(resourceName));

This would mean much smaller code since you don’t have to set up everything by hand, but I expect it to be slower. Whatever method you use, do it on a different thread, because that much instantiation would slow down your main thread in the beginning for sure.