Making waves of enemies?

Started by
19 comments, last by Serapth 12 years, 8 months ago
I havent written any code for this yet, i would just like to know how this is generally done. A problem i already ran into is when i tried doing this

if( monster->IsDead() )
delete monster;


I realised that I dont know what to do when there is more then one monster. Actually, at the moment im writing this i had an idea but i would like to hear from you guys first before i go trying anything and have to restart.
Advertisement
You can store any number of monsters in a container class like std::vector.
Im gonna need some help here. Why doesnt this work?



monster = new Monster( GameEnvironment );
monsters.push_back( monster );
When you delete the monster you need to make sure the monster isn't being used anymore. If you store pointers to the monster you can should get rid of all pointers to the deleted monster.

EDIT: is monsters of type std::vector<Monster*>?
It is now lol.

E: when the monster is deleted, i get an access error here...



void Graphics::FreeSurface( SDL_Surface* surface)
{
SDL_FreeSurface( surface );
}

Okay i hit another problem. When the monster dies i delete him, right? But then i get the access violation when all his functions are still being called.


Welcome to the wonderful world of programming in C++, sit back and enjoy the ride!

To avoid this problem, many developers use some form of memory management, reference counting for example is a very popular one which is also rather simple to implement

Each object which needs to be managed holds a counter (we call this a reference counter), and at the creation of your object this counter is set to 1
Besides this, these objects have 2 methods, we could call them addReference() and dropReference() for example; these methods are very simple, the addReference() method increases the reference counter by 1, and the dropReference() method decreases the counter by 1
The dropReference() method also does a check whether the reference counter is 0, when this condition is met the object gets deleted

All objects interacting with a reference counted object can now call addReference() when they need access to this object, and dropReference() when they're done with the object; this more or less guarantees that an object remains valid as long as it is needed by another object

You could apply this principle to your monster class and you could introduce a state indicating a monster is dead; when objects are interacting with a dead monster object they can drop their reference which will cause your monster to be deleted safely after the last reference has been dropped

Be aware of the fact though that this system is not perfect, some problems like cyclic references (2 objects referencing eachother which can make it impossible to get their reference count to 0) could occur

I gets all your texture budgets!

Here's what i was thinking Radikalism:

- Keep a counter for each monster that is alive
- For each new monster add 1 to the counter
- when a monster dies subtract 1 from the counter
- then where i need, make an if statement saying, if counter is >= 0, do this

Right now, i just need to figure out why im getting the access violation on SDL_FreeSurface();

Right now, i just need to figure out why im getting the access violation on SDL_FreeSurface();


Well most likely it's because SDL_FreeSurface * is trying to access an address that no longer exists ?

Perhaps you deleted the address to it's surface as well.
Can c++ crash my computer? Because im about to test my code and im scared it will break my comp lol

Can c++ crash my computer? Because im about to test my code and im scared it will break my comp lol


In theory I guess it could, but since memory used by each process is isolated to that process only by the operating system it is very unlikely ;)

I gets all your texture budgets!

This topic is closed to new replies.

Advertisement