I am developing 2d game, where maps are tile based, small about 20x20 tiles and player can travel between a lot of maps (in my game rooms) like this. The maps are connected sometimes verticaly and sometimes horizontaly and together they make one big cave.

Should I create a class where one 2d array holds all the maps (another 2d arrays), or is there any other way? Whats the most efficient way to create them in this class? Thank you for your help.


1 Answer 1


Use a 2D array that links a set of coordinates to a map object. When you get down to it, the only operation you'll be performing here is "get me the map at coordinates (X,Y)" and a 2D array is in a very literal sense made for that.

Each map should also know its own coordinates so that when the player exits right, you can say "get me the map at coordinates (X+1, Y)".

To make it easy to move maps around, map objects should not know their own coordinates until the first time they're requested from that 2D world array. At that point, the 2D world array tells it its coordinates.

A space concern

Since you seem to be making a winding cave network, you may become concerned once you run into a situation like this:

Lots of wasted space in the array, right? Only a fraction of coordinates are used. At this point, you're only wasting, maybe, a few kilobytes of data on unused coordinates. At this scale you shouldn't even be remotely concerned. If, however, the space wastage somehow becomes an issue, you can potentially trim a tremendous amount of fat by splitting your world into regions:

All the black space: Some saved kilobytes.

Each region is a small world in itself. To pass between regions, a region transition would say "go to region X, coordinates (X,Y)." Since your regions have no particular arrangement in space, put them in a 1D array.

  • \$\begingroup\$ Very nice answer. \$\endgroup\$ May 16, 2011 at 1:32
  • 1
    \$\begingroup\$ Looks like µF on the top :) \$\endgroup\$
    – House
    May 16, 2011 at 3:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .