(question) Tile mapping: 1d, 2d, or 3d Array?

Hello, I’ve recently started programming a game, (I mean, VERY recently). I have just gotten to the ‘tile map creation’ phase, and realized that there are many different methods to choose between.

The way i originally planed to do it, and the way I learned how to do it, was to just have all the tiles in 1 Array, and just augment their position once the array reaches a certain point (say, tile 7 or 8") by shifting the x and y coordinates.

However, i realized that method might not be the best suited for what i am planning to do.

The game i am starting to program is going to have multiple layers, having 2 layers for each type of ‘tiles’ or image needed:
1 & 2 background/panorama
3 & 4) base tiles (the ground/water)
5 & 6) item tiles/walls/ things that need a see through background

events/mobs just have their on x&y as they are thier own arrays

I need this because i am essentially making a 2d world sim game. you can build, you can farm, you can change the landscape to fit whatever you want, blah. (no, not like terreria/starbound. I’m not making a clone of them. This is styled like an old fashioned rpgs. Like a sandbox version of the old 2d harvest moon games)

However, the way i was taught how to do this, was to just do a completely different Array for each layer, but i was thinking, what about 2d arrays and 3d arrays? would one of them be easier to keep track of/program, and would one be faster for a pc to read?

So, my question(s) are:

Should i do multiple 1d arrays, multiple 3d arrays, or 1 big 3d array?
Which is easier to keep track of?
Which is faster for PCs/Macs?

I would think: 2D game -> 2D arrays. More layers ? ok then a list of 2D Arrays
I think its more intutive that way

faster? well that entirely depends on how you do it / implementation / if that created overhead / more elements you have to iterate over

One thing I have learned is not to worry too much about performance unless you know it’s going to be an issue (e.g. bad algorithm scaling). Instead, when you start off, do what reduces the conceptual weight of the project, what makes it easier to wrap your head around, as that is more important. If you can’t figure out how to do something at all, then what is the point of making it fast?

From what I got from this, I think the 3D arrays are the way to go, either one big one, or 3 separate ones.

example:

tiles[x][y][z]

where (x, y) is the world coordinate, and z is the layer you want to reference (0-5).

or you could have it [Z][X][Y], or any other order for that matter, just be consistent in the code haha!

Yeah, after giving it some thought, I’ve decided to go with this. I realized, after a bit, that doing a 1d array would make trying to program the chunk loading a pain, and having multiple 2d arrays would probably just waist time, as a 3d array does the same thing, and takes less coding.

Anyway, thanks everyone for your impute. :smiley:

If you have a layered map, you’ll probably want something like this:

[icode]for (Layer layer: Layers) { layer.draw() }[/icode]

Then your Layer class could contain an array of tiles, or for sparse layers that only have a few objects, you could always use a list or map. So this kind of implies “list of 2d arrays”.

if you create huge tile-maps think if chunks:


class Level{
Chunk[][] chunks;
}

class Chunk{
int[][] tiles; //you could also use short for saving some ram
LinkedList<Entity> entities;
}

So you are able to check what chunks are “active” has to be drawn -> draw tiles/entities.