I’m trying to basically make a system which allows me to load in multiple types of enemies. One type of enemy requires more elements than the others. So for example.
XDocument doc = XDocument.Load("Levels\\Level"+ levelIndex + "\\Waves" + levelIndex + ".xml");
List<WaveInfo> waveInfo = new List<WaveInfo>();
waveInfo = (from wave in doc.Descendants("wave")
select new WaveInfo()
{
enemiesInfo = (from i in wave.Descendants("enemy")
select new EnemyInfo()
{
type = Convert.ToInt32(i.Element("type").Value),
colour = Convert.ToInt32(i.Element("colour").Value),
speed = (float)Convert.ToDouble(i.Element("speed").Value),
spawnTime = (float)Convert.ToDouble(i.Element("spawnTime").Value),
}).ToList()
}).ToList();
This works fine and can load both types of enemy in. But in my xml I’m having to add a colour element to an enemy type that doesn’t require it. What I would like to happen is that with one type of enemy I can just not bother writing the colour tags and the serializer just processes that as a null value. How can I do this?
This should work:
Or, better yet you can use an explicit operator, like this one, like so: