Jump to content
Sign in to follow this  
lato1982

How to count AI soldiers kills?

Recommended Posts

Hi,

I'm trying to count kills for each soldier of my squad. I want to increase my AI soldiers skills dependend on number of their kills. I have a fixed number of AI soldiers in my squad, each one has an identity.

I know I have to use addEventHandler "killed" for all enemy soldiers, but not sure how to use the information about the killer name, to change a variable like ai01_kills_number?

Share this post


Link to post
Share on other sites

the callback used in the killed eventhandler will give you an array with the killer and the killed unit as a parameter.


friendlyAIUnits = units (group player);
killedEH = {
   _killed = _this select 0;
   _killer = _this select 1;

   _numKills = (_killer getVariable ["killsNum", 0]) + 1;    
   _killer setVariable ["killsNum", _numKills, true];

   if (_killer in friendlyAIUnits) then
   {
       // the killer is one of your AI members, do whatever you feel fancy here
       // preferably calling another function to add skill points or whatever.
   };
};



{
   if (!(isPlayer _x) && !(_x in friendlyAIUnits) ) then
   {
       _x addEventHandler ["killed", killedEH];
   };
} forEach allUnits;

using the above code will ensure that no matter who kills AI players, they'll get their kill counter increased.

So using something like

_numKilly = theUnit getVariable ["killsNum", 0];

on any AI or human unit in your game will get the corresponding kill counter.

  • Like 1

Share this post


Link to post
Share on other sites

I can't figure out why the code above works well, but this won't:

killedEH = {
   _killed = _this select 0;
   _killer = _this select 1;

   _numKills = (_killer getVariable ["killsNum", 0]) + 1;    
   _killer setVariable ["killsNum", _numKills, true];
_killer setSkill ((skill _killer) + 0.01);

   if (_killer in friendlyAIUnits) then
   {

// hint str _numKills;

   };
};

soldier1 addEventHandler ["killed", killedEH];

changing last lines to this and all works again, why?:

friendlyAIUnits = units (group player);

{
   if (!(isPlayer _x) && !(_x in friendlyAIUnits) ) then
   {
       _x addEventHandler ["killed", killedEH];
   };
} forEach allUnits;  

I want to add this event handler to specific soldiers, but cannot somehow. Any ideas?

Edited by Lato1982

Share this post


Link to post
Share on other sites

The forEach-Loop causes that everyone except of your soldiers will count towards the kill counter, hence the query if _x is not in friendlyAIUnits.

What you actually do is limiting the counter to soldier1, so if soldier1 dies, the one killing him will get his counter upped.

If soldier1 is someone in your team, this would have the opposite effect of the one you wanted to have.

I guess you want to adjust who actually can increase his kill counter, am I right? If that's the case, you have to change the if-clause in the eventhandler to make sure that the _killer is (for example) soldier1.

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
Sign in to follow this  

×