Archive for the ‘XML’ Tag

Parsing XML on the iPhone

Since I am developing a game engine that I would like to re-use, I have chosen to host all game data in the form of XML files. There are various reasons for doing this, partly because I can make game editors (Map Editor, Item Editor, NPC Editor, etc) and partly because I value using re-useable code. 

In order to get it all working, I had to explore the NSXMLParser given by the iPhone SDK. With XNA and the .NET framework, I got used to reading in XML in a very specific way, and the iPhone does it quite differently. Currently, my test map’s XML file looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<Map>
	<MapName>Test Map</MapName>
	<MapContentName>Map001</MapContentName>
	<MapDimensions>
		<MapWidth>5</MapWidth>
		<MapHeight>5</MapHeight>
	</MapDimensions>	

	<TileDimensions>
		<TileWidth>32</TileWidth>
		<TileHeight>32</TileHeight>
	</TileDimensions>	

	<SpriteSheetName></SpriteSheetName>
	<MapLayers>
		<BaseLayer>0, 1, 0, 0, 0, 0, 0, 0, 0</BaseLayer>
		<MiddleLayer>0, 1, 0, 0, 0, 0, 0, 0, 0</MiddleLayer>
		<TopLayer>0, 1, 0, 0, 0, 0, 0, 0, 0</TopLayer>
		<AtmosphereLayer>0, 1, 0, 0, 0, 0, 0, 0, 0</AtmosphereLayer>
		<CollisionLayer>0, 0, 0, 0, 0, 0, 0, 0, 0</CollisionLayer>
	</MapLayers>
</Map>

In order to read in the XML, there are 3 methods that you need to implement. Since your XML parser goes line by line, you will need to write a method that starts an element, ends an element, and reads a character. Such as:

// Start of element
- (void)parser:(NSXMLParser *)parser 
			didStartElement:(NSString *)elementName 
			namespaceURI:(NSString *)namespaceURI 
			qualifiedName:(NSString *)qName 
			attributes:(NSDictionary *)attributeDict
{
}

// Found Character
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSMutableString *)string 
{
}

// End Element
- (void) parser:(NSXMLParser *)parser 
			didEndElement:(NSString *)elementName 
			namespaceURI:(NSString *)namespaceURI 
			qualifiedName:(NSString *)qName
{
}

Once you have these methods set up, all you really need to do is populate them. For instance, in the “Start Element” method, if your “elementName” = “MapName” (in the above XML case) you would allocate the map classes NSString like so;

mapName = [NSMutableString string];

When you found a character, you would store those into a string, and when the end of the element is reached, you would put that string into whatever element it was reading. For example, the map name:

mapName = stringValueFromFoundCharacter;

Not too hard, but wasn’t easy to figure out either.. At least I know it and now can implement readers for every data that I wish to store as XML!

RPG Editor: Exporter Settings – First Preview

With the first version of the RPG editor just about ready to be tested, I thought I’d take the opportunity to share some of the things we’d like to implement. So lets take a look at some of the custom XML exporting!

From this first screen shot, you will notice that on the left half of the form is all of the fields that can be changed, while each change will be reflected by the XML Output on the right. Using this data, it will be easier to see how your final XML will look, before you export the XML file. 

You can also see that we’re trying to make any map created with our editor be directly “plug and play” with an XNA project. For example, the XnaContent tag is automatically implemented, and so is the Asset Type= but the user will be able to specify their project namespace as well as the map class name that will be housing the data in-game. Obviously every XNA project needs this information unless you are using a custom importer / processor for your XML data, so you can not opt out of having this information exported.

Attempting to make the XML as flexible as possible, you will notice one feature we’ve already put in. You can specify the “Output Type” of your data to take advantage of the various importing techniques of the XNA Content Pipeline. For example, if you wish to import your “MapDimensions” data as a point, the output XML will look something like:

<MapDimensions>X Y</MapDimensions>

while if you wish to import it as a custom data and write your own Content Reader / Writer for that data, you can and it will export something like:

<MapDimensions>
     <Width>
X</Width>
     <Height>
X</Height>
</MapDimensions>

and naturally, all fields are customizable so if you wish to change “Width” to “MapWidth,” you can.

So, for now… this is a small snippet of what we’re working on. Again, we expect this project to be open source once we get further down the road, so anyone can take what we’ve done and expand on it or use it for their own editors.

I hope this project encourages the creation of some great RPGs, because I love me some RPGs :) See you all soon!

Content Creation

With the Dream Build Play project “Complete,” I’ve turned my attention to a few items that will aid in the creation of not only Meir, but any future RPG I write. Content creation is a tricky subject, as the goal is to create something flexable enough to re-use in any RPG project, but dynamic enough and easy enough to use so if I had a few people working on any given project, they would know what to do.

Well me and a buddy from the shacknews chatty decided that he didn’t want to use TileStudio anymore, but wanted something more suited for his game.. so we started colaborating on an editor that would be built specifically for XNA games, but flexable enough to use in any project. Obviously this editor will have to support several major features of basic RPGs, but how in depth are we going to really go with this thing?

Create and edit tile maps? Basic feature, has to be included.
Exporting XML in the same style of the XNA Content Pipeline? Basic feature, has to be included.

but what about features like adding a “tile” to the texture, and having the map build the texture and exporting that to a PNG file? What about creating a system that can export to multi dimensional or single dimensional arrays based on the project needs? How about a dynamic XML system that lets the user edit how their XML should look, then hitting “Export XML” and having their XML formatted correctly? There are many many more ambitious goals we wish to include, and we’re thinking a bit larger scale then just these… but its pretty interesting to see what we could come up with. As always, I’ll keep everyone up to date on whats actually going on. 

So if you had to pick and choose a few “Must Have” features of a game editor, what would you want included?

ShackRPG: Updated Tile Engine Part 2

With the Map class complete, we can no start working extra things into our Tile Engine to display each map to screen. In its current state, the tile engine is incomplete for a few reasons including collision detection between characters and drawing order of all layers. However, i do have enough to get the basics of the engine up and running and continue to work on the rest of the systems of the game. So, on that note, lets begin!

I have 6 seperate regions to help organize the tile engine;
TileEngine Data – Houses all data associated with the engine. 3 data fields and properties; a true / false statement on if we’re loading a new map into memory, a Map file, and the mouse location for the editor.

Constructor(s) – initializes the tile engine on startup

private void Initialize()
{
map = new Map();
map.Load(“Map001″);
}

Update / Draw – … can you guess?
Update right now does nothing, but will call the “CheckCollisions();” method.
Draw just calls “map.Draw()” … so thats very easy.

Clamp Camera – Aah this is a fun one. I’ll post the code and see if you can make heads or tails of it.

public void ClampCameraToBoundries(Vector2 position)
{
//clamps the camera to the players position
Globals.Camera.Position.X = position.X – (Globals.Graphics.GraphicsDevice.Viewport.Width / 2);
Globals.Camera.Position.Y = position.Y – (Globals.Graphics.GraphicsDevice.Viewport.Height / 2);

//Clamps the camera within the game screen
Globals.Camera.Position.X = MathHelper.Clamp(Globals.Camera.Position.X, 0, (map.Width * map.TileWidth) – Globals.Graphics.GraphicsDevice.Viewport.Width);
Globals.Camera.Position.Y = MathHelper.Clamp(Globals.Camera.Position.Y, 0, (map.Height * map.TileHeight) – Globals.Graphics.GraphicsDevice.Viewport.Height);
}

Make sense? Basically you give the method the players current positional vector and it keeps the player within the center of the screen. If the player moves right, the camera moves right. If the player moves left, the camera left. However, after that it keeps the camera on top of the game world so if the player moves left to the edge of the map, the camera will stop when it reaches the imaginary edge.

Player Movement – This region does 4 things and is quite long compared to the camera clamping method. It will take the keyboard data collected by the games Update() method to determine if the player should be moving, and which direction. It then takes the positional vector of the player, and the speed modifier of the player, and calculates what the new position will be. Finally, it will take that new position, check to see if the player is on a tile it can walk on, and if so passes the new position to the game for update. If the player is on a tile they shouldn’t be on, it passes the old position, in essence, the player has not moved. Confusing huh? It can be.

Save / Load / Create New Map – map editor functions, and these are quite long and deal with loading and saving from an XML file from within the content pipeline. I do not write my own content importers or processors, so I’ve been using the System.XML namespace. If you want to know how its done, inquire within!

I tend to organize my programs by clumping things together. I know some people do “Data fields” “properties” “Constructor(s)” “Public Methods” “Private methods” but in bigger programs I’ve gotten into the habit of clumping everything together that goes with eachother. Might be a bad habit but to each his own. I’ve spent a few days on other systems for the Tile Engine and I’ve updated the code at the projects site. Next update I’ve had a request to post about my particle effects system, so I’ll get that in order and have it out here in a few days. I also will go into my in-game map editor.

As always, if you’re interested in checking out the full source code (newly updated), just head on over to code.google.com;

Follow

Get every new post delivered to your Inbox.