3D Arrays

Aug 19, 2011 at 4:26pm
Hi all,

I'm trying to make a RPG game inside a console application and so far it's going great with events and programmable text boxes. But to add more rooms and maps I need a 3d array. So I could type something like this:

1
2
3
4
5
6
7
8
9
10
11
char Map1[10][20] = {}; // Map 1 goes here

char Map2[10][20] = {}; // Map 2 goes here

char Maps[10][20][50] // Same as before but made to hold 50 maps
for(int y = 0; y < 10; y ++){ // Make a 20 x 10 grid in computers mind
for(int x = 0; x < 20; x ++){
Maps[y][x][0] = Map1[y][x]; // save that tile from the drawn map to char maps
Maps[y][x][1] = Map2[y][x];
}
}


Once again, if I can get this to work it will save me 200 lines of code for each map I want to use. The error my compiler throws out at me when I do this is:
Error: Invalid conversion from char(*) 20 to char[20]


Can anybody help me with this, and if you can't can you give me some suggestions on what else I could try.

Thanks all.

EDIT 1 --- I've already screwed my whole code over anyway so I may as well start again if nobody can help in any way.
Last edited on Aug 19, 2011 at 4:45pm
Aug 19, 2011 at 5:42pm
Which line gives you that?
Any way, memcpy, or use vectors instead of arrays.

Or you could avoid all the copying by making Maps an array of pointers. I honestly have no clue about how to write a pointer to a 2d array, so I would suggest to wrap that 2d in a struct:
1
2
3
4
struct Map{
   char data[20][10];//this might as well be data[200]. It doesn't make much difference.
   char& get( int x, int y);
}

You can then still do the = {} initialization, but you can't do [][] access, so I added a getter.
Maps would then be
1
2
Map* maps[50] =
   {&map1, &map2};


Or you could (should) store all the maps in a file and read only the one you need thus not having a 3d array at all.
Aug 20, 2011 at 4:50am
The thing is, I haven't got that far for any of the crap you just mentioned. I've barely been reading through my book. I just need a way to make a 3d array and put the maps into it.

Once I get to saving and editing files in my book I will try to make a map editor and a way for the engine to read those maps and put them into the array.
Last edited on Aug 20, 2011 at 4:52am
Aug 20, 2011 at 6:39am
Ok. But still, which line does the error come from? I don't see anything wrong in the snippet you posted (except for a missing ; on line 5).
Topic archived. No new replies allowed.