0

I've been working on a tile based map engine for my game project in Xna C#. The system, like most others, uses a digit corresponding to a tile in a tileset mapped to a specific position on screen. This works fine, but requires every cell on screen to have a number manually entered. Instead, I've decided to have level layouts read from a .txt containing the number of each tile in the position it would be ingame, like so:

1111
0110
1001
1100

Where 1 is grass and 0 is dirt. Again, I'm aware this is a common technique. The code I have written can read each line and set the next position in the first column to the corresponding tile graphic. This is fine, but it does not help with the rest of the map. I've been searching and cannot find how you would split each number in a row into a separate number, so that the first line would read (0,0) = 1, (0,1) = 1, etc, so I can then match the coordinates to the x and y position on the map, and the value to the type of tile.

So what I need is the ability to assign a 2d array corresponding to the current position (how many characters left in the file, how many lines down in the .txt file), so I can just run two branched for loops (x and y) for every tile in the level ie:

for (x=0; x<levelwidth; x++)
{
    for (y=0; y<levelheight; y++)
    {
        Row[x].Column[y].Tile = Convert.ToInt32(filepos[x,y]);
    }
}
3
  • Please don't add things like "C#" to the ends of your titles. That's what the tags are for. Feb 11, 2012 at 18:40
  • sorry I was only trying to make it clear which language. Feb 11, 2012 at 19:49
  • As I said, the tags make it clear. Feb 11, 2012 at 23:07

3 Answers 3

1

You don't want to use 2D arrays because of heavy performance issues.

Also, you probably want to use a separator between tile numbers, like this

1,1,1,1
1,0,1,1

for two reasons; 1 you can use more than 10 different tiles, and 2 you can then use String.Split() and Int.Parse() in order to get your tile IDs and build your map.

In order to use a 1D array, instead of doing myMap[x][y], you do myMap[y*mapWidth+x].

4
  • Care to expand on the heavy performance issues with 2d arrays? Are you sure that manually indexing a 1d array will bring you any benefit?
    – Mark H
    Feb 11, 2012 at 18:50
  • Multidimensional array lookups are usually orders of magnitude slower than working with a regular array in C#, and yes, the manual indexing brings a lot of benefit. The type of array OP is talking about is actually a "jagged" array ([][]) instead of a multi-dimensional one ([,]), but there are still very noticeable performance gains from manually indexing a 1D array. I do believe this is somewhat improved in .NET4 though...
    – Tobias
    Feb 11, 2012 at 19:00
  • splitting the string was just what I needed, and you have a point about the number limit on my algorithm, I didn't really think about that. I won't use a 2d array for the time being. thanks. Feb 11, 2012 at 19:51
  • Another way to avoid the number limit is to use special characters for different blocks. For instance, make '_' a platform or 'O' a block, and ' ' just air. Makes it a bit easier to visualize your level in the editor and provides as many block values as there are ascii characters.
    – A-Type
    Feb 12, 2012 at 21:03
0

I switched to using XML so that I could more easily edit and read the file. That also gave me a great way of indicating the end of a row of tiles in my map as well as an easy way of identifying up front just how many columns/rows my map had prior to the reader loading it in the game.

The sample for the code can be found on my site in the "Looks Level to Me" sample and it may be that it will help get some ideas for how to change your current approach.

2
  • I'll have a look, thanks. I'm not sure I really want to work with XML at the moment, but I'm sure i will in future Feb 11, 2012 at 19:52
  • Keep in mind that XML is a format that takes a long time to parse, increasing load times.
    – Tobias
    Feb 11, 2012 at 20:05
0

While the commas are the best approach you can also use .substring to take it apart if things are of fixed length.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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