Page 1 of 1

[SOLVED]Loading a map crashing the game.

Posted: Mon May 23, 2011 9:36 pm
by like80ninjas
Okay so I have a function that loads a map, and it basically clears three vectors, and then loads things back into those vectors.

Now, if it loads the same map over and over, it clears and loads them fine, but if I change it to load a different map than the start map it crashes at the ThinkAll function,(Which calls the think function of every entity in the Entitys vector)


This is the entire function for loading a map, it's used at the start of the game to load the beginning map and anywhere else.
It loads blocks into vectors, and then at the end creates a test warp and then the player.

ALL collision Blocks (soldtile, jumptile) are in the entity vector, and in their respective vectors. The player, warps and normal tiles are in the entity vector.

Code: Select all

void Map::Load(string filename, int start_x, int start_y )
{
	SolidTile::SolidTiles.clear();
	for (int i = 0; i < SolidTile::SolidTiles.size(); i++)
	{
		SolidTile::SolidTiles.at(i) = NULL;
	}
	JumpTile::JumpTiles.clear();
	for (int i = 0; i < JumpTile::JumpTiles.size(); i++)
	{
		JumpTile::JumpTiles.at(i) = NULL;
	}
	Entity::Entitys.clear();
	for (int i = 0; i < Entity::Entitys.size(); i++)
	{
		Entity::Entitys.at(i) = NULL;
	}

	string line, filepath, filepathfile, filepathprop;

	filepath = "Data/Maps/" + filename;

	filepathfile = filepath + "/" + filename + "_prop";

	cout << "Loading Map properties from file: " << filepathfile << "\n";
	ifstream myfile (filepathfile.c_str());
		getline(myfile,line);
		width = atoi(line.c_str());
		getline(myfile,line);
		height = atoi(line.c_str());
	myfile.close();
	cout << "Map Width: " << width << "\n";
	cout << "Map Height: " << height << "\n";


	filepathfile = filepath + "/" + filename + "_layer0";

	cout << "Loading Map collision layer from file: " << filepathfile << "\n";
	myfile.open (filepathfile.c_str());

	for (int i =0; i<height/32; i++)
	{
		for (int r = 0; r<width/32; r++)
		{

			getline (myfile,line);
			if (atoi( line.c_str() ) == 1)
			{
				new SolidTile( r*32, i*32);
			}
			if (atoi( line.c_str() ) == 2)
			{
				new JumpTile( r*32, i*32);
			}
		}
	}

	myfile.close();

	filepathfile = filepath + "/" + filename + "_layer2";

	cout << "Loading Map tile 2 layer from file: " << filepathfile << "\n";
	myfile.open (filepathfile.c_str());
	
	int tile_x, tile_y;

	for (int i =0; i<height/32; i++)
	{
		for (int r = 0; r<width/32; r++)
		{
			getline (myfile,line);
			tile_x = atoi(line.c_str());
			getline (myfile,line);
			tile_y = atoi(line.c_str());
			if (tile_x!=-1 && tile_y!=-1)
			{
				new Tile( r*32, i*32, tile_x, tile_y );
			}
		}
	}

	myfile.close();

	filepathfile = filepath + "/" + filename + "_layer1";

	cout << "Loading Map tile layer from file: " << filepathfile << "\n";
	myfile.open (filepathfile.c_str());


	for (int i =0; i<height/32; i++)
	{
		for (int r = 0; r<width/32; r++)
		{
			getline (myfile,line);
			tile_x = atoi(line.c_str());
			getline (myfile,line);
			tile_y = atoi(line.c_str());
			if (tile_x!=-1 && tile_y!=-1)
			{
				new Tile( r*32, i*32, tile_x, tile_y );
			}
		}
	}

	myfile.close();

	
	new Warp( 512-32, 64, "small", 64, 64, 1 ); 


	Player::GetInterface()->SetNull();
	Player::GetInterface()->Spawn( start_x, start_y );
}

Re: Loading a map crashing the game.

Posted: Tue May 24, 2011 4:20 pm
by bnpph

Code: Select all

   SolidTile::SolidTiles.clear();
   for (int i = 0; i < SolidTile::SolidTiles.size(); i++)
   {
      SolidTile::SolidTiles.at(i) = NULL;
   }
   JumpTile::JumpTiles.clear();
   for (int i = 0; i < JumpTile::JumpTiles.size(); i++)
   {
      JumpTile::JumpTiles.at(i) = NULL;
   }
   Entity::Entitys.clear();
   for (int i = 0; i < Entity::Entitys.size(); i++)
   {
      Entity::Entitys.at(i) = NULL;
   }
The for loops are pointless, as .size() will return 0, because of clear()

Also, why are you doing this?

Code: Select all

 
        if (atoi( line.c_str() ) == 1)
         {
            new SolidTile( r*32, i*32);
         }
I don't see any reason why you are using the "new" keyword. Especially since you are not passing any references in the constructor for the this pointer to be stored in. You are likely getting some nasty memory leaks, or are doing something in a bad way.


Also, I don't see "ThinkAll" function in the code - did you leave something out?

Re: Loading a map crashing the game.

Posted: Tue May 24, 2011 10:49 pm
by like80ninjas
Thanks for the reply, I already solved the problem though. As for calling new, those are the blocks that make up my map in game. They are put into the SolidTile vector by their constructor.