# I seem to have hit a snag in my map loader

This topic is 4818 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

In short, I need a way to create a 2 dimensional array of a size that isn't known (The map size) and then set the size when it is known (In the map class's constructor) I've tried using a 2D vector, but this got confusing since I've never had to use vectors before. Is there any other way? Or should I just make it so that maps have a fixed size (Not my favorite solution, but it WOULD work...)

##### Share on other sites
Of course there is a way, but im making an asumption here that that is a std::vector container, therefore in C++ ;)

int * MapArray;//Allocation MapArray = new int[Height * Width];// Itherationfor(int x = 0; x < Width; ++x) for(int y = 0; y < Height ; ++y)  MapArray[y * Width + x] = FooBar;//Deallocationdelete[] MapArray;

or

int ** MapArray;//AllocationMapArray = new int*[Width];for(int i = 0; i < Width; ++i) MapArray[i] = new int[Height];// AccessMapArray[5][5] = FooBar;// Deallocationfor(int i = 0; i < Width; ++i)   delete[] MapArray[i];delete[] MapArray;

IMO A stl vector is better, typesaftey ;)

-Decsonic

##### Share on other sites
i would do it like our good friend 'Anonymous Poster' said. firstly however, you would have to load some sizes from a file (say SizeX, SizeY) and then (id use the second example by AP. this way you know exactly which tile you want to do something with - you dont need to work it out from a 1d array) use those sizes to create the 2d array of map data/tiles.

kinda like this little alogrithm-ish thing!:
int sizex, sizey;
int **mapdata;
Load MapSizeX (from file) into sizex;
Load MapSizeY (from file) into sizey;
Create mapdata array (same as AP using sizex and sizey as the sizes for the array);
Load the tiles from the file into mapdata array using 2 'for' loops, one for each dimension of the array:
for(int x = 0; x < sizex; ++x)
for(int y = 0; y < sizey; ++y)
mapdata[x][y] = --whatever the current tile in the file is (so like tile[x][y] from the file)--;

thats about it for loading maps. man i rambled on a fair bit there! sorry! hope you understood it all - thats the basic idea i used for loading the maps with my editor that im making and it works fine! ;)

let us know if ur still having probs. ;)
the_moo

##### Share on other sites

This topic is 4818 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.