Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am using XNA 4.0 and am currently working on a simple tile game to pass the time. Basically, I'm using the tried and true method of having numbers refer to tile types, where in 0 would be an empty tile, 1 would be grass, et cetera.

This is all well and good and works fine but I was wondering how to incorporate other assets. For example, a map may have parallax backgrounds, background music, and of course the tileset from which the tile map refers to draw its textures.

Ignoring ambient music and gratuitous scrolling backgrounds, I am still unsure of how to implement just the joining of the tile map (text) and tileset (png image). I see several solutions:

1) Embed the texture data for the tileset directly within the map file. This could be extended to other assets as well and would allow for all map files to be together in a single file. Unfortunately, the file sizes quickly become massive as raw texture data is several times larger than compressed images. For this I'm using GetData and WriteBase64.

2) Somehow mention the directory or filename of the tileset from within the text map file. This is how I typically see it done, but because I am using the content loader and not simply loading off the hard drive, this may not be possible. Perhaps the asset name could be stored but this solution seems a bit error-prone.

3) Completely separate the textual map data from the tileset image. At runtime, first load the map tiles and then allow any sort of tileset image to be loaded afterwards. This has the advantage of different map files using the same assets without duplicated data and very small file sizes. However, with this solution the width of the tileset would still have to be known before hand in order to correctly offset tile values. Also, map files may become lost and more easily edited by the user.

4) Create some sort of ZIP derivative in which all map files are contained in this package. Then, the textual map file can refer to the assets within this container. I'm not exactly sure whether or not this would work with XNA's content loader or how to implement it either, so I'm not sure of the this solution.

How do most developers typically solve this problem with XNA?

share|improve this question

5) Don't roll your own. .NET in general has really great serialization functions, and XNA is no exception:

Edit: As far as WHERE to store them, they should be resources in a class library, and however you serialize your data should refer to them using reflection (I typically also have some sort of easier to use string alias as well, because reflection names are ugly).

share|improve this answer
I'm not really sure if this answers my question considering I'm wondering where to store assets and not how to store them. Also, I'm not really sure where serialization's role is in storing large assets like music and tileset textures. Anyways, I don't really see how maps can be created with this approach, only saved and loaded. – Chris Copeland Mar 9 '12 at 18:01
Edited with other suggestions. The rest is all up to how you structure the classes to hold the game data. – PlayDeezGames Mar 9 '12 at 18:45

Somehow mention the directory or filename of the tileset from within the text map file. This is how I typically see it done, but because I am using the content loader and not simply loading off the hard drive, this may not be possible. Perhaps the asset name could be stored but this solution seems a bit error-prone.

Check out the ExternalReference class. You can load an external Texture2D by its file name relative to the location of the file currently being processed. So as long as the text file that stores the tile data stores the location of the tile map you are good to go.

share|improve this answer

In my game I have several layers of tiles to achieve the parallax effect. Each layer is a 2D array of a minimal Tile class. If the array element is null, there is no tile there. The tile class stores an enum that refers back to a tile template class, which might be Lava or Wall, or Grass, etc. The template tells me all of the attributes that tiles of that kind share, like what the texture & u,v coordinates are, whether it's walkable by default, what its vertex colors should be. In your case this might also include what music should be playing. The individual tile objects in my layer array can override the template values to do something interesting. All the tiles in a layer use the same texture atlas so rendering them is just one batch but you could just as easily use multiple textures. My layer texture is loaded by the ContentManager and assigned to the layer when my level initializes. When drawing, I interleave the layers with the game objects (characters, monsters doors, chests) that occupy each layer for some simple occlusion.

share|improve this answer

Unless I misunderstand, you are asking how to make a 2D tile engine. This tutorial will walk you through the process, among other things.

Most developers do not solve this problem. They use an existing tile engine. Several exist for XNA.

If you feel compelled for some reason to make your own, I think the best option would be to have a Tileset object, then have each tile know its tile code, and have the draw method look at each tile, get its code, get that code's tile graphic from the tileset and draw.

As for specifying a tileset, it should be enough for each map to specify a tileset code, and the the Tileset object can fetch the relevant tile sheet for that code.

In any event, the thing you appear to ignore is: You should find a map editor you can use, and then code such that you are compatible with it, thereby saving yourself the trouble of writing your own editor later. The way the map editor does will probably immediately suggest simple answers for your questions.

share|improve this answer
Pretty sure he has a 2D tile engine up and running since he is asking what is the best way to wave/load the data while using the content pipeline. – ClassicThunder Mar 9 '12 at 17:52

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.