Jump to content

  • Log In with Google      Sign In   
  • Create Account


Game Object Management (Deletion)


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
4 replies to this topic

#1 bd36   Members   -  Reputation: 118

Like
0Likes
Like

Posted 18 March 2012 - 01:46 PM

Hello,

I am working on creating a relatively simple tower defense type game as a project to really learn some game development and some c++ stuff i haven't ever really used to accomplish anything other than like an example or tutorial. So far I have gotten the map to load from a file and the enemies to follow the path set by the map. However, I can't figure out how to delete the enemies when they get to the end. Currently, I have them just now displaying anymore once they reach the end. However, once I add the towers and they are attacked I think it will be more important for enemy to actually be removed from the enemy array. I am not sure how I should go about doing this though. Below is my main game loop. Any suggestions or suggested reading would be greatly appreciated. I could really find anything applicable on Google.

#include "SDL.h"
#include "Timer.h"
#include "globals.h"
#include "MapSquare.h"
#include "Map.h"
#include "Enemy.h"
#include <string>
#include <vector>
#include "GameObject.h"
int main(int argc, char* args[])
{
init();
bool quit = false;
Timer fps;
//load level
Map level("map1.dat"); //load the level
std::vector<Enemy> enemies; //hold all the enemies on the map
Timer enemySpawn(1500); //new enemy spawns every 1.5 seconds
int numEnemies= 0;
//While the user hasn't quit
while( quit == false )
{
  fps.start();
  //------------------------------------------------INPUT------------------------------------------------------------------------------------------------------
  //While there's events to handle
  while( SDL_PollEvent( &event ) )
  {
   //If the user has Xed out the window
   if( event.type == SDL_QUIT )
   {
	//Quit the program
	quit = true;
   }
  }
  //------------------------------------------------LOGIC-------------------------------------------------------------------------------------------------------
  for(unsigned int i = 0; i < enemies.size();i++)
  {
   enemies[i].update(&level); //update the state of all the enemies currently on the map
  }
  //check if time for new enemy
  enemySpawn.update(); //update the spawn counter
  if(enemySpawn.check() && numEnemies < level.getNumEnemies()) //if time for a new enemy and less then max
  {
   enemies.push_back(Enemy(level.getStart().getX(),level.getStart().getY(),2.5,1000,Enemy::Green)); //add enemy to the map
   numEnemies++;
  }

  //------------------------------------------------DISPLAY------------------------------------------------------------------------------------------------------
  level.show(); //show the map
  for(unsigned int i = 0; i < enemies.size();i++)
  {
   enemies[i].show(); //show each enemy
  }
  SDL_Flip(screen);
  //Cap Framerate
		if( fps.get_ticks() < 1000 / FRAMES_PER_SECOND )
		{
			SDL_Delay( ( 1000 / FRAMES_PER_SECOND ) - fps.get_ticks() );
		}
}

return 0;
};


Sponsor:

#2 teutonicus   Members   -  Reputation: 518

Like
1Likes
Like

Posted 18 March 2012 - 09:54 PM

A simple way to do this could involve just removing 'dead' enemies from your enemies array, like so:

typedef std::vector< Enemy > EnemyArray;
EnemyArray enemies;
...
main loop
{
	 ...
	 // your enemy update loop
	 for ( EnemyArray::iterator enemy = enemies.begin(); enemy != enemies.end(); )
	 {
		  enemy->update( &level );
		  if ( enemy->isDead() )
		  {
			   // enemy is dead, erase it
			   // NOTE: vector::erase will return an iterator to the element following the element it removes. so you do not want to increment it
			   // in this loop iteration; doing so will cause you to skip updating an enemy or even iterate past enemies.end(), which is obviously bad
			   enemy = enemies.erase( enemy );
		  }
		  else
		  {
			   ++enemy;
		  }
	 }
	 ...
}

You'll also no longer need to loop and 'show' all of your enemies every frame with this solution (I'm assuming that 'show' is where the magic logic to hide dead enemies currently happens).

There are possibly better/more efficient ways of doing this (calling vector::erase could call a lot of assignment operators, which could be expensive), such as storing an array of Enemy shared_ptrs rather than Enemy objects, reusing Enemy objects stored in a pool, etc. But when starting out it's usually better to KISS; you will discover/learn these techniques as you actually need them.

#3 bd36   Members   -  Reputation: 118

Like
0Likes
Like

Posted 19 March 2012 - 07:11 PM

Thank you very much for the reply. I tried doing something similar but not using an iterator, so when I deleted an object the vector would change and I was throwing all sorts of out of bounds errors. Im going to do some more reading on iterators tonight to try and really understand what is going on. But would it be correct to say that by accessing via an iterator rather than the [ ] operator you are getting a pointer to that instance of the Enemy class and that is the reason it is necessary to use -> rather than .?

Also am curious as to way you say I do not need to "show" each of the enemies every frame now? How else would I display them? I thought it was bad practice to put the display of the enemy in the update loop. I was told that the best way to structure the game loop was handle input, update everything for movement, collisions etc then finally at the very end display the results. Here is are the current functions for showing and updating the enemies. (Previously there was just and if statement before apply_surface to check if alive)
void Enemy::show()
{
getSpriteClipping();
apply_surface(x,y,image,screen,&currentImageClipping);
};

void Enemy::update(Map* level)
{
direction = level->getSquareType(x,y,direction);
move();
}
void Enemy::move()
{
if(direction == MapSquare::Right || direction == MapSquare::Start)
{
  x = x + speed;
}
else if(direction == MapSquare::Left)
{
  x = x - speed;
}
else if(direction == MapSquare::Up)
{
  y = y - speed;
}
else if(direction == MapSquare::Down)
{
  y = y + speed;
}
}


#4 teutonicus   Members   -  Reputation: 518

Like
1Likes
Like

Posted 19 March 2012 - 07:46 PM

But would it be correct to say that by accessing via an iterator rather than the [ ] operator you are getting a pointer to that instance of the Enemy class and that is the reason it is necessary to use -> rather than .?

Essentially, yes.

Also am curious as to way you say I do not need to "show" each of the enemies every frame now? How else would I display them? I thought it was bad practice to put the display of the enemy in the update loop. I was told that the best way to structure the game loop was handle input, update everything for movement, collisions etc then finally at the very end display the results. Here is are the current functions for showing and updating the enemies. (Previously there was just and if statement before apply_surface to check if alive)

void Enemy::show()
{
getSpriteClipping();
apply_surface(x,y,image,screen,&currentImageClipping);
};

Oops, my bad. I was assuming incorrectly that 'show' did something else, such as setting an 'I am visible' flag or something, not the rendering of your enemies. Please ignore my comment about it. Your game loop looks fine.

#5 bd36   Members   -  Reputation: 118

Like
0Likes
Like

Posted 19 March 2012 - 08:21 PM

Thanks for the help!




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS