Jump to content
  • Advertisement
Sign in to follow this  
Sir Sapo

Getting rid of Pointer Objects

This topic is 5413 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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];

Share this post


Link to post
Share on other sites
Advertisement
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?

Share this post


Link to post
Share on other sites
hmmmm... i'm not sure on syntax.
but i believe it's:

// explosion or death animation

delete 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?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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 + 1], i - --EnemyCount);
EnemyList = (Enemy **) realloc(EnemyList, EnemyCount * sizeof(Enemy *));
}

Share this post


Link to post
Share on other sites
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 value
public:
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();
}


Share this post


Link to post
Share on other sites
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 ...

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!