• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
bd36

Game Object Management (Deletion)

4 posts in this topic

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.

[code]#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;
};[/code]
0

Share this post


Link to post
Share on other sites
A simple way to do this could involve just removing 'dead' enemies from your enemies array, like so:

[code]
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;
}
}
...
}
[/code]

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.
1

Share this post


Link to post
Share on other sites
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)
[code]void Enemy::show()
{
getSpriteClipping();
apply_surface(x,y,image,screen,&currentImageClipping);
};[/code]

[code]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;
}
}[/code]
0

Share this post


Link to post
Share on other sites
[quote name='bd36' timestamp='1332205861' post='4923482']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 .?[/quote]
Essentially, yes.

[quote]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)
[code]void Enemy::show()
{
getSpriteClipping();
apply_surface(x,y,image,screen,&currentImageClipping);
};[/code]
[/quote]
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.
1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0