I have a tile map deffined as an array something like this:
const int mapSizeX=29, mapSizeY=12;
int map[mapSizeY][mapSizeX]={ 1, 1, 1, 1, ..etc };
then I have my second map:
const int map2SizeX=20, map2SizeY=11;
int map2[map2SizeY][map2SizeX]={ 0, 0, 0, etc... };
during runtime I need to swap these, or more exactly I only need the content of the second map2 to become the first map.
any idea how I could do this? I have no clue how to change the size of an array. if I remove "const" from "int mapSize" it it will not compile and then I cant change the size of a constant :(
how do I need to declare it then if I want to be able to change the size during the runtime?
your second solution would work only for this small example but if I would need a bigger array to copy over this would not work.
(if I would have a third map3[30][20] for example which I would want to be the next time map)
hmm.. maybe I could deffine the first map with maximum possible map size and then just copy over any needed arrays which will always be smaller... and ignore the rest. something like map[100][100]
Just a suggestion. If you use one of the C++ container classes, such as std::vector, you might suffer some small performance reduction, but I think it would make this easier to manage. http://cplusplus.com/reference/stl/vector/
Dynamic memory:
Declare them as pointers and use new[] to allocate space for them. http://cplusplus.com/doc/tutorial/dynamic/
You would need to delete[] the memory at the end of the program. This could make the swap easier as you could possibly just swap the pointers instead of elements of the arrays.
ok i have tried the dynamic memory solution with pointers (my head always hurts when I try to use pointers)
here is how I declare my variables:
1 2 3 4 5 6 7 8
int mapSizeX, mapSizeY;
int *map;
constint map1SizeX=29, map1SizeY=12;
int map1[map1SizeY][map1SizeX]={
constint map2SizeX=20, map2SizeY=11;
int map2[map2SizeY][map2SizeX]={
map1 and map2 are my 2 arrays which I would like to copy over to the map during the runtime
so I have tried to copy map1 like this:
1 2 3 4 5 6 7 8 9 10 11
mapSizeX=map1SizeX; // set the size of my map array
mapSizeY=map1SizeY; // set the size of my map array
map = newint[mapSizeY][mapSizeX]; // initialize the map array with these values
// fill the map array with values from map1 array
for (int n=0; n<mapSizeY; n++)
{
for (int m=0; m<mapSizeX; m++)
{
map[n][m]=map1[n][m];
}
}
all this looks like its working (no compiler errors)
but the problem is that I have a loads of errors for the rest of my code everywhere where I try to use my map array... like for this:
if (map[bbox4y1][bbox4xM]==33)
compiler says: error C2109: subscript requires array or pointer type
fatal error C1003: error count exceeds 100; stopping compilation
(i get a lot of "subscript requires array or pointer type" errors)
I didn't quite understand... do you just want to swap out the complete content, or do you want to swap out the content and keep the size of both arrays?
oh it works now!
I forgot to add that another * in my map declaration like prophetjohn wrote:
int * * map;
map = new int*[mapSizeY];
now the program compiles ok
btw why are there 2 "* *" I don't get that declaration...
2_hanst99:
I would like to keep my map1 and map2 untouched.. they act like blueprint of my level.
and all I want is to copy content from map1 or map2 into my "map" array which is the array that my program uses to render the level on the screen, for collisions, etc...
and so I can swap the level on the go by simply copy another array into it (and change its size).. like when player enter door for example then I would copy content of map2 into map so the whole level will change (but map1 and map2 will stay untouched)
map = newint*[mapSizeY]; creates an array of pointers to ints of size mapSizeY, you then make each of those pointers point to a new array of integers. if you only had one asterisk, you would be trying to use an integer to hold the address of an array of ints, which you can't do
thanks,
pointers that points to another pointers that points to somewhere thats why my head hurts when I just think about it :)
the code is working but there is another problem that surfaced now...
if (shoot==3 && (map[shootMapY][shootMapX]==1 || map[shootMapY][shootMapX]==2))
this is line from my code that checks if my shot projectile hit a wall (==1 and ==2)
the problem is that whenever the value of "shootMapY" is below 0 or more than maxSizeY of the array the program crashes!
which is strange because when the value of shotMapX is below 0 or above the maxsize of an array it works fine!
I must say when I used the normal array (not as pointers) i didn't have these problems.
might this have something to do with these pointers?
below zero and greater than maxSizeY is not within the bounds of the array. you're likely accessing memory that doesn't belong to you, so the OS is terminating the program. gotta check the bounds
the memory just behind or in front of the indices corresponding to shootMapX may still belong to the program, so it runs (probably with garbage) and the OS doesn't terminate it