Getting rid of Pointer Objects

Started by
8 comments, last by invective 19 years, 6 months ago
In my game I have a class called Enemy that makes, obviously, an enemy. I have an array of Enemies to facilitate collision detection, movement, etc. How would I get rid of the Enemy objects in the array? The array looks like this: Enemy *enemies[5];
My Current Project Angels 22 (4E5)
Advertisement
why not dynamically allocate the Enemies?

for (int i = 0; i < 5; ++i) {   enemies = new Enemy;}


would something like that work?

Beginner in Game Development?  Read here. And read here.

 

Yeah , when I load up a level, the enemies are made by a loop like you have , my problem is when an enemy is destroyed, how do I remove it from the array?
My Current Project Angels 22 (4E5)
hmmmm... i'm not sure on syntax.
but i believe it's:
// explosion or death animationdelete enemies; //i being which enemy is it obviously


i take it you have some way of tracking them right?
or is that the real question?

Beginner in Game Development?  Read here. And read here.

 

I guess this is what you want, though a linked list or some STL container might be easier to handle.
int EnemyCount = 0;Enemy **EnemyList = NULL;void CreateEnemy(){ Enemy *p = new Enemy(); EnemyList = (Enemy **) realloc(EnemyList, ++EnemyCount * sizeof(Enemy *)); EnemyList[EnemyCount - 1] = p;}void RemoveEnemy(int i){ delete EnemyList; memmove(&EnemyList, &EnemyList, i - –EnemyCount);<br> EnemyList = (Enemy **) realloc(EnemyList, EnemyCount * <span class="cpp-keyword">sizeof</span>(Enemy *));<br>}<br><br></pre></div><!–ENDSCRIPT–> 
here it is with STL
#include <iostream>#include <vector>class enemy{	//you better provide a copy constructor and assignment operator if you	//use pointers. x is a dummy valuepublic:	int x;};void delete_enemy(std::vector<enemy> & e, const unsigned int position){	//Bounds check	if (position >= e.size())		return;	std::swap(e.at(position), e.back());	e.pop_back();}void print_enemies(std::vector<enemy>& e){	for (std::vector<enemy>::iterator i = e.begin(); i<e.end();++i)		std::cout << (*i).x <<std::endl;}int main(){	std::vector<enemy> enemies;	enemy e;	for (int i=0; i<4; ++i)	{		e.x=i;		enemies.push_back(e);	}	std::cout << "These are the members before deletion:\n";	print_enemies(enemies);	delete_enemy (enemies, 2);	std::cout << "These are the members after deletion:\n";	print_enemies(enemies);	std::cin.ignore();}
If you are going to remove Enemies from the middle of the vector, you should think of using list instead.
a vector is ideal when the total size of the list is going to be mostly fixed, or at least fixed except while switching levels ...

a list is ideal when you need to add and remove items for any location in the list, and when you only do forward and reverse traversal (no random access)

a deque is ideal when your list needs to change size and you also need random access. deques are only optimized for adding / removing from the beginning and end ... BUT, if your list does not need to retain the same order there are tricks to make deletions from the middle very efficient as well ...
The first ap was me. Hopefully this will teach me not to rely on browser cookies in the future..

Quote:Original post by Eriond
If you are going to remove Enemies from the middle of the vector, you should think of using list instead.
Somehow I doubt that's a problem when you're dealing with 5 entries ;)
Then again a little room for expansion never hurts, this might just become the foundation of a particle engine.
Quote:Original post by Eriond
If you are going to remove Enemies from the middle of the vector, you should think of using list instead.


You can always remove from the middle of a vector by swapping the item to be removed with the item at the end -- which is exactly what my code does. Only use a list if you care about the order of you items, because obviously the swap screws it up. Otherwise the vector is faster and has less memory overhead.

This topic is closed to new replies.

Advertisement