[SOLVED]Loading a map crashing the game.

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
like80ninjas
Chaos Rift Regular
Chaos Rift Regular
Posts: 101
Joined: Thu Dec 09, 2010 2:13 am

[SOLVED]Loading a map crashing the game.

Post 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 );
}
Last edited by like80ninjas on Tue May 24, 2011 10:49 pm, edited 1 time in total.
User avatar
bnpph
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 75
Joined: Thu Mar 10, 2011 12:30 pm

Re: Loading a map crashing the game.

Post 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?
like80ninjas
Chaos Rift Regular
Chaos Rift Regular
Posts: 101
Joined: Thu Dec 09, 2010 2:13 am

Re: Loading a map crashing the game.

Post 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.
Post Reply