Game Development Reference
In-Depth Information
Now, let us continue in our Main() method to parse the remaining rows and
create objects for each (see Listing 12.4).
}
}
// Grab the first row of data
node = node.NextSibling;
while (node != null)
{
CharacterData data = new CharacterData();
data.character = node.ChildNodes[fields["Character"]]
.InnerText;
data.description = node.ChildNodes[fields["Description"]]
.InnerText;
data.hitPoints = float.Parse(node.ChildNodes[fields
["HitPoints"]].InnerText);
data.moveSpeed = float.Parse(node.ChildNodes[fields
["MoveSpeed"]].InnerText);
data.attackPower = float.Parse(node.ChildNodes[fields
["AttackPower"]].InnerText);
data.characterClass = (CharacterClass)Enum.Parse(typeof
(CharacterClass), node.ChildNodes[fields["Class"]]
.InnerText);
data.thumbnail = node.ChildNodes[fields["Thumbnail"]]
.InnerText;
characters.Add(data);
node = node.NextSibling;
}
}
// Our xml is fully parsed now, so we can do whatever we need
// with the data
foreach (CharacterData data in characters)
Console.WriteLine(data);
}
}
Listing 12.4. C# code to parse the remaining rows (parsexml.cs).
If we're loading many of these objects and runtime performance is an issue,
then we can serialize these objects to disk in a binary format to avoid parsing
the XML document at runtime. If performance isn't an issue, then we can use the
XML documents as-is. It is also possible that for developer builds we can just use
the XML directly to allow quick reloading of objects and write out final formats
only on release builds.
With the code we have just written, we have now made part of the game data-
driven, allowing a designer to tweak values in the game from a spreadsheet. While
this was not too dicult, it begs the question: is there an easier way?
Search WWH ::




Custom Search