Followers 0

# problems with initializing my map

## 6 posts in this topic

Hey guys,
so I am working on a simple level editor for a platform game I&#39;m making, and everything&#39;s been going smoothly up until now.
I decided I wanted to have a 3d vector to allocate tiles for my editor instead of a 3d pointer because they are very difficult to understand( for me anyway). The level allocates correctly ONLY if the width, height, and layers of the level are all the same value, and that is what is stumping me.
I would greatly appreciate it if someone could help me out.

//My vector container
vector<vector<vector<Tile*> > >map;

//How I'm allocating the map... in the Level constructor
Level::Level(int tileWidth, string tilePath)
{
sheet.Init(tileWidth, tilePath);

for(int l = 0; l < layers; l++)
{
map.resize(layers);
cout << "Layer " << l << endl;

for(int i = 0; i < height; i++)
{
map[i].resize(height);
cout << endl<< "Row " << i << endl;
for(int t = 0; t < width; t++)
{
map[i][t].resize(width);
map[i][t].push_back(new Tile(t * sheet.GetTileSize(), i * sheet.GetTileSize(), false, sheet));
cout << "X " << t << endl;
}
}
}

}

//How i am saving the level -- using integers to represent tile types
void Level::Save()
{

cout << "NOPE" << endl;
ofstream file(fileName.c_str());
for(int x = 0; x < layers; x++)
{
file << endl << endl << "Layer: " << x + 1 << endl << endl;
for(int y = 0; y < height; y++)
{
file << endl;
for(int z = 0; z < width; z++)
{
file << map[x][y][z]->GetType();
}
}
}

}



0

##### Share on other sites

I tried that, and it had the same result as before. It only works when the width, height, and layers are all the same. When, for example, i set the width and height to 10, and the layers to 3, it returns an error that says: vector subscript is out of range.

0

##### Share on other sites

Last line should be

map[l][i].push_back(...)


Copy-paste error on my part.

1

##### Share on other sites

Thank You Brother Bob. I still have much to learn.  But then again, we're never done learning are we?

1

##### Share on other sites

Nor should we be done learning.

2

##### Share on other sites

If you resize in the 'i' loop, instead of reserve, you don't need to push back, you can just use the indices directly map[l][i][t];

Probably makes it clearer what is going on.

Virtual spanks all round for using lower case L as a variable name, looks too much like the number 1. (EDIT: I'd use layer, row, col instead for the index variables I think).

I'm not jumping for joy about naming the array vector3 'map' either, since that is also the name of a class in namespace std.

1

## Create an account

Register a new account