Tonight, I decided to go ahead and work on the Level Editor some more, so that I can start churning out some levels for a playable demo. In Resistor, I used Mappy to generate arrays that I hardcoded as constants in my level class. This probably wasn’t the best coding style, even though I’d bet it is faster than loading from disk. In addition, I want players to be able to create their own levels in Blasting Bits, which means that having constant arrays is not an option.
To keep my data clean and organized, I decided to go with the XML format. I was glad to find out that XNA has methods for handling reading XML files. I found a good tutorial video by Richard Key of Paranoid Ferret Productions on how to do this. I tried using this to load a String object from XML stored in my Content project area and it worked perfectly. The “Boo” message that I entered into the XML file was loaded into the String variable I created using the Content.Load message, similar to a Sprite, Font, or Model.
However, I quickly realized that there is a problem when trying to create an XML data file for one of my own custom classes. When trying to set the asset type to one of my classes, it is not able to find my class. I created a simple Foo class with a Sting and int instance variables. At first I thought the problem was due to my class not being public, but changing the scope of the class made no difference. Doing a quick web search showed that this problem has been encountered before. After digging deeper and consulting the video mentioned before, I believe that the reason why it can’t find my class is because the Content project doesn’t have a Reference defined to my main game project. I tried adding a Reference, but it doesn’t allow this because it says that it would create a circular reference. The only solution I was able to find is to create a third project just to hold the XML files. This seemed really excessive, and this method probably won’t support loading XML files generated by the player so I found another approach.
After finding a great example written by Microsoft, I was able to take a new Foo object and serialize it. In the example, it writes the XML to the Console. Unfortunately, that isn’t very helpful aside from displaying the serialized object. After some more trial and error, I found that instead of passing Console.Out to the Serialize method I could instead pass another Stream. At first I tried using StreamReader and StreamWriter, but both of those require a filename, while I just want to store it as a String. Finally, I found the StringWriter class, which takes no parameters and can hold the value of the serialized object. The XML can be extracted using the ToString() method on the StringWriter object. I was able to display the serialized data on the level editor screen. From there, I just pass the String to my FileSaver class in ResistorKit, which handles opening and writing to the storage device.
That was enough to melt my brain for one night, so I will continue on later with reading the XML value back into my game object. Last night I was able to get the remainder of my armor models (helmet, hands, boots, chest) to display in the game. I also got the models for the basic collectible for money and the gun to display as well. For now, I just used the second level helmet as the enemy model. All of these models continually rotate during gameplay.