# Making waves of enemies?

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

## Recommended Posts

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.

##### Share on other sites
You can store any number of monsters in a container class like std::vector.

##### Share on other sites
Im gonna need some help here. Why doesnt this work?

 monster = new Monster( GameEnvironment ); monsters.push_back( monster ); 

##### Share on other sites
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*>?

##### Share on other sites
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 ); } 

##### Share on other sites

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

##### Share on other sites
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();

##### Share on other sites

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.

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

##### Share on other sites

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

##### Share on other sites

[quote name='nano511' timestamp='1313088219' post='4847823']
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 ;)
[/quote]

Okay, i trust you. But if anything goes wrong...

##### Share on other sites
Okay i thought this would make 2 monsters, but i think it is infinitly looping
 for( int i = 0; i <= 1; i++ ) { monster = new Monster( GameEnvironment ); monsters.push_back( monster ); } 

##### Share on other sites

Okay i thought this would make 2 monsters, but i think it is infinitly looping
 for( int i = 0; i <= 1; i++ ) { monster = new Monster( GameEnvironment ); monsters.push_back( monster ); } 

Your for-loop seems correct, so it shouldn't be looping infinitely, maybe there's something wrong with the constructor of your monster class?

##### Share on other sites
im not sure whats happening... If i take it out of the loop it still doesnt work?

##### Share on other sites
I usually make a class
 class Monster { ...whatever I need for it bool alive; }; ... Monster monster[How many I need or might need]; 
I set all the monsters that I create as alive = false initially then I use alive to keep track of each that is alive setting it to true, and before creating a new one I cycle through starting at the beginning to the end and use a the first Monster that is dead and replace it with one that is going to be alive. If I happen to reach the end that first one will be dead and I just make it alive.

##### Share on other sites

im not sure whats happening... If i take it out of the loop it still doesnt work?

I found the problem, it was in a different for loop further down. But, is this wrong?

 for( int i = 0 ; i <= monsters.max_size(); i++) { monsters->Move( player->xPos ); } 

I want it to loop for as many monsters there are inside the vector

##### Share on other sites

[quote name='nano511' timestamp='1313089192' post='4847833']
im not sure whats happening... If i take it out of the loop it still doesnt work?

I found the problem, it was in a different for loop further down. But, is this wrong?

 for( int i = 0 ; i <= monsters.max_size(); i++) { monsters->Move( player->xPos ); } 

I want it to loop for as many monsters there are inside the vector
[/quote]

Dont use vector::max_size, that is the maximum size the construct can hold, not the actual size. Use vector::size() instead.

##### Share on other sites
I switched it to .size() but it still says the iterator is going too high or what ever.
 for( int i = 0 ; i <= monsters.size(); i++) { monsters->xVel = 0; } 

##### Share on other sites
and don't use <=. Sometimes compiler warn you when you compare signed integers with unsigned integers so I often use std::size_t in these cases.
for(std::size_t i = 0 ; i < monsters.size(); i++) { monsters->Move( player->xPos ); } 

NVM i got it

##### Share on other sites

maybe this will help you guys find the problem better. This is all the work i do with the vector before any for loops
 std::vector< Monster* > monsters; ... monster = new Monster( GameEnvironment ); monsters.push_back( monster ); 

Did you implement what wooh suggested, you are going 1 over the bounds of your array with <=, as C++ arrays are 0 based. ( In other words, an array of 10 items starting at 0 would be 0,1,2,3,4,5,6,7,8,9 )