# Dynamic sized arrays for map files

## Recommended Posts

Liam M    223
Ok, im working on a tile based engine, and am currently constructing a map editor. The engine works with a maximum of five rendering layers, but how many layers the map uses is stored in the map file, so if you want a heavy duty/light fast map, ou can have it. But since the engine also supports maps of ANY size, i need to make it so the map editor can create an array of anysize. Iv read the article on dynamically sized arrays, but that didnt help what so ever. How would i go about creating an array thats size can be set by using 3 varibles in the Map structure, EG: Town.MapDATA[Map.XSizeInTiles][Map.YSizeInTiles][Map.LayerDepth] Any help at all would be appreciated, cheers

#### Share this post

##### Share on other sites
overflowed_    194
What I usually do is just make a normal array that's the total size of whatever you're trying to allocate:

ie:
int *Array = new int[mapWidth * mapHeight * mapLayers];

And whenever I'm accessing members, use:

Array[y*mapHeight*mapLayers + x*mapLayers + layer]

And you've got your data!

#### Share this post

##### Share on other sites
Balrog30    157
While I have also used the above method, the simplest way (for your engine) may be to use a standard fixed-size array, and declare the size of the array AFTER you have loaded the map. This method, however, does not work well for editors, as the size of your map would either have to be completely fixed, or you would have to redeclare the variable every time you load. This method does have the advantage of being easier to trace, and the code is also a little easier to adapt to new projects fairly quickly. Feel free to tell me if my idea is garbage, since I've only done 2 tile-based games so far. (It's a little far off from your question, but this method was very easy to adapt to the pathfinding agent I made using the A* algorithm, since the graph was already present. The path node locations simply matched the location on my map grid, so if I moved the location of the grid in my interface, it did not affect my path-finding agent at all. A pixel-based navigation system would have been a mess.)

#### Share this post

##### Share on other sites
another way is the ugly yet flexible std::vector of vectors of vectors of tiles....

typedef std::vector<std::vector<std::vector<Tile> > > TileMap

TileMap my_map;

my_map[l][x][y] = blah;

where l is the layer, and the x/y is the coord in tiles.

#### Share this post

##### Share on other sites
by the way, you might want to re-think your design. if your trying to put layers into your game, you do not need X amount of multi-dimensional arrays to have X layers. instead, you can have a tile decide how many layers it has and the info for that layer. this saves a lot of memory since its a good chance that a lot of the tiles in all of those layers arent being used, and are just taking up memory. if a tile knows how many layers it has, then you only use up the memory for each layer of each tile.... ok, its kind of hard to explain... heres what i mean:

struct Tile_Layer{  int layer;  Image image;}struct Tile{  unsigned long flags;  //the layers this tile has. it could be 0 or as many as you want  std::vector<Tile_Layer> layers;};

then you make a regular 2 dimensional array of Tile... you can give each tile as many layers as you want... then, in your rendering loop you do something like this:

for(each y){   for(each x)   {       for(each lower layer in map[y][x])       {          Draw your bottom layers here for tile in map[y][x], say you had 5 layers, numbered 0-5, you would draw layers 0,1,and 2... or, whatever bottom layers you want... you could make all the layers top layers, or 1 bottom and 4 top, or 2/3, whatever..       }    }}here you draw the player and all other game objectsfor(each y){   for(each x)   {       for(each upper layer in map[y][x])       {         here you draw the upper layers. for example 3 and 4.       }    }}

just something you might consider, it would save a lot of memory and allow you to make huge maps without worrying about memory problems as much.

#### Share this post

##### Share on other sites
Liam M    223
thank you all, im gonna use the vector of vectors method, and il also rethink my layering engine

## Create an account or sign in to comment

You need to be a member in order to leave a comment

## Create an account

Sign up for a new account in our community. It's easy!

Register a new account

## Sign in

Already have an account? Sign in here.

Sign In Now