Jump to content
  • Advertisement
Sign in to follow this  
mypel16000

Killing AI

This topic is 2070 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

Hi there,

 

I am making a game of zombies and I am trying to make them die when I hit them. I have tried different methods, but they never work.

I just need some concepts to base my code on.

 

What I thought was:

 

having :          if the bullet hits the zombie, take 10 away from its health;

another if:      if health < 0, make a bool variable "Dead" true;

My zombies are stored in a vector, so:

 

for (int i = 0 ;  i < zombie.size() ; i++ )

{

      if( zombie[ i ].Dead )

     {

          zombie.clear( zombie.begin() + i )

     }

}

 

But this didn't work, I shot one and it died, but when I shot the 2nd one, all of the zombies on the screen died. How can I do this effectively?

 

 

P.S: I have tried simple deleting the object using delete zombies[ i ], but the program just stops responding

Share this post


Link to post
Share on other sites
Advertisement

I've tried, but the game "stops responding" when you use delete zombie[ i ]. Has it got something to do with its members or its functions or.... does it need a destructor?

Share this post


Link to post
Share on other sites

[quote name='mypel16000' timestamp='1358719100' post='5023641']
I've tried, but the game "stops responding" when you use delete zombie[ i ]
[/quote]

Could your array be going out of bounds?

Share this post


Link to post
Share on other sites

Isn't a zombie already dead?

 

(sorry for lame joke :P)

 

Anyway, you'd want to set it up so as soon as the zombie's health reaches 0 (not < 0, but <= 0) the game sets it as "dead".

Share this post


Link to post
Share on other sites

The problem is that you're modifying the very collection that you're iterating through. Every time you delete something from a collection that you're going through, the size will change, but not in the loop, so original size (i.e. 10) will now be one less, (i.e. 9), and when you get to the last original index (i.e. 10), you'll access random location, and usually crash the app.

Save indexes of zombies to delete in another collection, and delete them in one consistent loop after the loop you're doing now.

Share this post


Link to post
Share on other sites

This isn't working on my code. I believe I have got to the problem, and it is the bool "dead". I can't seem to fix what is wrong though.

 

What I first do is check to see if health is less than zero by looping through the objects in my vector:       zombies[ i ].checkHEalth();

If it returns a 0 or less, i change a public bool in the object ( dead ) to true. Then I loop through them again and zombies.clear() the ones that return true for dead. The problem is that on debugging the code, i find that when one dies all of the bools in the loop turn to true, and therefore they are all deleted.

 

Any more ideas about this

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!