Sign in to follow this  
nano511

Making waves of enemies?

Recommended Posts

nano511    103
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
[code]
if( monster->IsDead() )
delete monster;
[/code]

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 this post


Link to post
Share on other sites
nano511    103
Im gonna need some help here. Why doesnt this work?

[code]

monster = new Monster( GameEnvironment );
monsters.push_back( monster );
[/code]

Share this post


Link to post
Share on other sites
Wooh    1088
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 this post


Link to post
Share on other sites
nano511    103
It is now lol.

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

[code]

void Graphics::FreeSurface( SDL_Surface* surface)
{
SDL_FreeSurface( surface );
}
[/code]

Share this post


Link to post
Share on other sites
Radikalizm    4807
[quote name='nano511' timestamp='1313028791' post='4847454']
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.
[/quote]

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 this post


Link to post
Share on other sites
nano511    103
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 this post


Link to post
Share on other sites
lukeymoo    106
[quote name='nano511' timestamp='1313030159' post='4847466']
Right now, i just need to figure out why im getting the access violation on SDL_FreeSurface();
[/quote]

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 this post


Link to post
Share on other sites
Radikalizm    4807
[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
[/quote]

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 this post


Link to post
Share on other sites
nano511    103
[quote name='Radikalizm' timestamp='1313088362' post='4847826']
[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
[/quote]

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 this post


Link to post
Share on other sites
nano511    103
Okay i thought this would make 2 monsters, but i think it is infinitly looping
[code]

for( int i = 0; i <= 1; i++ )
{
monster = new Monster( GameEnvironment );
monsters.push_back( monster );
}
[/code]

Share this post


Link to post
Share on other sites
Radikalizm    4807
[quote name='nano511' timestamp='1313088792' post='4847831']
Okay i thought this would make 2 monsters, but i think it is infinitly looping
[code]

for( int i = 0; i <= 1; i++ )
{
monster = new Monster( GameEnvironment );
monsters.push_back( monster );
}
[/code]
[/quote]


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 this post


Link to post
Share on other sites
BrianJensen    343
I usually make a class
[code]
class Monster
{
...whatever I need for it
bool alive;
};

...
Monster monster[How many I need or might need];
[/code]
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 this post


Link to post
Share on other sites
nano511    103
[quote name='nano511' timestamp='1313089192' post='4847833']
im not sure whats happening... If i take it out of the loop it still doesnt work?
[/quote]



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

[code]

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



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

Share this post


Link to post
Share on other sites
Serapth    6671
[quote name='nano511' timestamp='1313089374' post='4847835']
[quote name='nano511' timestamp='1313089192' post='4847833']
im not sure whats happening... If i take it out of the loop it still doesnt work?
[/quote]



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

[code]

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



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 this post


Link to post
Share on other sites
nano511    103
I switched it to .size() but it still says the iterator is going too high or what ever.
[code]

for( int i = 0 ; i <= monsters.size(); i++)
{
monsters[i]->xVel = 0;
}
[/code]

Share this post


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

Share this post


Link to post
Share on other sites
Serapth    6671
[quote name='nano511' timestamp='1313090384' post='4847846']
maybe this will help you guys find the problem better. This is all the work i do with the vector before any for loops
[code]
std::vector< Monster* > monsters;
...

monster = new Monster( GameEnvironment );
monsters.push_back( monster );
[/code]
[/quote]


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 )

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