• Advertisement
Sign in to follow this  

problems with initializing my map

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey guys,
so I am working on a simple level editor for a platform game I'm making, and everything'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)
{
	AskProperties();
	AskFileName();
	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();
			}
		}
	}
	
}


 

Share this post


Link to post
Share on other sites
Advertisement

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.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
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.

Edited by Paradigm Shifter

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement