Killing AI

Started by
33 comments, last by Mercile55 11 years, 2 months ago

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

Advertisement

Try using

delete zombie;

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

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?

i suspect the problem is in your code for setting the dead flag.

edit: nvm, thought u were using erase, not clear.

Check out https://www.facebook.com/LiquidGames for some great games made by me on the Playstation Mobile market.

the problem is in how I use the vectors I think, when I try to delete 1. all of the others are deleted,,,

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

Stay gold, Pony Boy.

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".

"I AM ZE EMPRAH OPENGL 3.3 THE CORE, I DEMAND FROM THEE ZE SHADERZ AND MATRIXEZ"

My journals: dustArtemis ECS framework and Making a Terrain Generator

Did you note the i--?

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.

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

This topic is closed to new replies.

Advertisement