Blog Archives
You have your base class of Baddie and built Goomba’s and Turtles with it and held it all in an array.
The question now is how do you save that array to XML?
Here is the Base and Super Classes I’ve made:
public abstract class Baddie
{
public Vector2 Position;
public Baddie()
{
Position = new Vector2();
}
public abstract void AI(float gameTime);
}
public class Goomba : Baddie
{
public Goomba()
{
}
public Goomba(Vector2 position)
{
Position = position;
}
public override void AI(float gameTime)
{
Position.X -= (gameTime * 10);
}
}
And here is the array and it being filled:
Baddies = new List<Baddie>();
Baddies.Add(new Goomba(new Vector2(100,30)));
Baddies.Add(new Goomba(new Vector2(300, 40)));
Baddies.Add(new Goomba(new Vector2(320, 40)));
Baddies.Add(new Goomba(new Vector2(340, 30)));
Baddies.Add(new Turtle(new Vector2(500, 50)));
Baddies.Add(new Turtle(new Vector2(700, 10)));
Baddies.Add(new Turtle(new Vector2(5000, 20)));
So to save this we need an array of Types that implement the base class Baddie, this can be done by hand or it can be done automatically via the following function:
public static Type[] GetAllTypesForCollection<T>()
{
List<Type> list = new List<Type>();
Assembly[] assemblies = System.AppDomain.CurrentDomain.GetAssemblies();
for (int i = 0; i < assemblies.Length; i++)
{
if (!assemblies[i].GlobalAssemblyCache)
{
Type[] types = assemblies[i].GetTypes();
for (int j = 0; j < types.Length; j++)
{
if (typeof(T).IsAssignableFrom(types[j]))
{
list.Add(types[j]);
}
}
}
}
return list.ToArray();
}
So the save code looks like this:
using (FileStream fs = new FileStream("test.xml", FileMode.Create))
{
XmlSerializer s = new XmlSerializer(typeof(List<Baddie>), GetAllTypesForCollection<Baddie>());
s.Serialize(fs, Baddies);
}
and the end XML like this:
<?xml version="1.0"?>
<ArrayOfBaddie xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Baddie xsi:type="Goomba">
<Position>
<X>100</X>
<Y>30</Y>
</Position>
</Baddie>
<Baddie xsi:type="Goomba">
<Position>
<X>300</X>
<Y>40</Y>
</Position>
</Baddie>
<Baddie xsi:type="Goomba">
<Position>
<X>320</X>
<Y>40</Y>
</Position>
</Baddie>
<Baddie xsi:type="Goomba">
<Position>
<X>340</X>
<Y>30</Y>
</Position>
</Baddie>
<Baddie xsi:type="Turtle">
<Position>
<X>500</X>
<Y>50</Y>
</Position>
</Baddie>
<Baddie xsi:type="Turtle">
<Position>
<X>700</X>
<Y>10</Y>
</Position>
</Baddie>
<Baddie xsi:type="Turtle">
<Position>
<X>5000</X>
<Y>20</Y>
</Position>
</Baddie>
</ArrayOfBaddie>
There are many ways to create 2D arrays in C# so I decided to take a look at 4 different methods and see which one was the fastest.
The 4 types that were chosen:
- List of lists(List< List < > >)
- Jagged arrays ([][])
- Multidimensional arrays ([,])
- 1D array using maths to find the place ((y * size) + x).
1000 x 1000 sized arrays were created and tested, these tests included Creation, Filling, finding 1 value (at position 500×500) and iterating across the whole array, time is in milliseconds.
Results
Windows7
List of Lists
Create: 0 Build: 29.0016
Find 1: 0 Iterate: 15.0009
Multi Dimensional
Create: 0 Build: 12.0007
Find 1: 1.0001 Iterate: 11.0006
Jagged
Create: 0 Build: 8.0004
Find 1: 0 Iterate: 6.0004
1D Array
Create: 0 Build: 8.0004
Find 1: 0 Iterate: 6.0004
WP7
List of Lists
Create: 64 Build: 261
Find 1: 1 Iterate: 142
Multi Dimensional
Create: 4 Build: 120
Find 1: 0 Iterate: 120
Jagged
Create: 0 Build: 64
Find 1: 0 Iterate: 46
1D Array
Create: 0 Build: 64
Find 1: 0 Iterate: 46
XBOX 360
List of Lists
Create: 5 Build: 185
Find 1: 0 Iterate: 139
Multi Dimensional
Create: 1 Build: 120
Find 1: 0 Iterate: 136
Jagged
Create: 1 Build: 64
Find 1: 0 Iterate: 62
1D Array
Create: 1 Build: 64
Find 1: 0 Iterate: 62
From the numbers is looks like C# internally works a Jagged array as a 1D array and does that math for us and so from a speed and easy of use point of view it seems Jagged arrays win out.
Huge thanks to MadNinjas of #XNA (irc.efnet.net) for running the code on the other platforms!
[edit]
It seems I had mixed up the multi dimensional and jagged arrays in my code so my numbers were wrong, this is now fixed.
I thought I would post up a quick tool I have written to make it easy to animate and create my sprites for my games.
This is written in C# and it using XNA for the drawing of the sprite example, time line and animation preview. It helps that I’m using XNA for my Indie game development. I’m also using the great Dock Panel to give me dockable forms.
For starting a tool like this I typically make sure I get the data class (at least the members) right then add the needed methods that are needed while working on the tool.
Now for saving and loading the created animations I just use XML, this because its really easy to serialise classes to XML in C# and it means if anything goes wrong its easy to open the file and edit it. As we know loading XML in our game can be slow so the best idea is to create a custom content importer to get it in game quickly.
So with out any more waffling here is the video (yes that is me talking):
So you have decided to make a computer game, that’s great the question is what game do you want to create first?
Most new developers will answer that they want to create the game they have always wanted to.
This is the first problem, as while you are full of enthusiasm and energy you also lack experience. Game development is hard and time consuming, far more then you would expect when first starting out and as such you are unlikely to pull off your perfect game first time, your first few games will end up buggy and barely working and most people give up before getting anywhere because they tried to do more then they are currently capable of.
So where do you start?
Do a small simple game, something that will allow you to learn the basics of game development with out the worry of trying to build your perfect game. This could be something as simple as pong or Tetris, which will teach you how to draw graphics, handle input and basics game systems.
Once you’ve finished your first few games you will not only have a better understanding of how to make a game but you will also have more enthusiasm for it.
This is going to be my new personal Blog.
I’m planning on using this to talk about games and game development, expect more in the future.
The game development posts will cover a wide mix of topics and development levels. I will be going from complete novice upto expert, I plan on tagging these to help out.
