Counting actor's deaths

I need to have a variable count the number of times a specific actor has died in battle, but cannot find how to do this.

Can anyone tell me how this is done?

Thanks.
I would start by counting how many times the death state has been removed, and adding one if the actor is currently dead.
That would be much easier to implement as it requires specific skills or items to remove the death state - much easier than trying to check for all forms of damage or continually checking if the death state is applied or removed.

If the actor is autorevived without specific skill/item use, then you either need a way to count the autorevive or you'll have to add a script changing the death state coding in the default engine.
@Andar No autorevive in this instance, so that's one complication less. Presumably the item, or skill, would have to call a common event which would run a conditional to see which actor it was being used on, then if it is the correct actor, add to a variable, and then do its reviving stuff.

Is that what you mean?
Basically yes, that is one way to handle it.
It could also be handled with code in the damage formula, but that would be a bit more complex depending on how many actors - but sometimes easier if there is a problem with using common events.
@Andar What would the code be in the damage formula? This would only ever apply to one particular actor, which should reduce the complexity, and might in the long run be easier than using common events.
Would be a really easy code snip, but I'm stuck at work for another three hours...

A damage formula isn't very reliable here since other calculations occur after the formula is evaluated. Guard rate, element rate, critical damage...
@Rinobi If you're prepared to work your scripting magic, I'm more than happy to wait.
Thank you.
You could use this:
https://forums.rpgmakerweb.com/index.php?threads/state-add-remove-commands.18593/

and in the death state's notebox add the onadd: to increment a variable if b is an actor and b.id (or b.actor_id) is the id of the specific actor you're interested in.
@Shaz So the full note would read
onadd: b.actor_id?(5); v(61) +=1

Something feels wrong about that, but I can't identify what it is.
there is a function that handles death all by itself.
just tap into that and run your counter from there, that way you can use the actor object's own id function to point to an array.

Code:
class Game_Battler < Game_BattlerBase
 alias die_stock die
 def die
  die_stock
  $game_variables[61][id] += 1 if actor?
 end
end

?
In battle event that plays during every action, have a variable branch that checks if the actor is knocked out, then another branch within it that checks if death switch is off. If off, turn it on and add +1 to death count. If the actor is not knocked out, turn the switch off.
You would need to make sure b is an actor, and not an enemy.

It would be something like:

onadd: \v[61] += 1 if b.is_actor? && b.actor_id == 5

I'm not sure about the method names or if \v is available in this script (I suspect it is, because I use b.-stuff- in one of the examples and where b is available, \v should be as well)

I'm on my work computer, so I can't check the syntax until a bit later.
@gstv87 and @Milennin
Thanks for these suggestions. However, having seen the script that Shaz linked to, I'm probably going to go with that, as I can think of a number of other uses for this script, once I've got the hang of the commands to put in the note box. It opens up a range of possibilities.

EDIT
@Shaz You posted just as I was typing my reply.
I've tested this, and no error message, but the variable was not increased.
okay - seems that was an old version where I had some issues with the order of things. Can you please download and try again?

This is a method in the battler class, so you can use any commands for a battler (ie - leave out the b. - it just works on the target by default).

The correct command to add to the state is:

onadd: $game_variables[61] += 1 if actor? && id == 5

However, in testing, I noticed a weird situation occurs - when the actor is first set up, their HP starts at 0 before it's initialized to the correct value. And the test to "apply the death state if HP is 0" gets run. So as soon as you start a new game, the death state is applied, the onadd script is run and the variable is incremented.

To get around this, you just need to set the variable to 0 at the start of the game (or at the time you add the actor to your party and initialize them).

Seems to work after that.
@Shaz Tested and it works perfectly for me. Once more you come to my rescue.
Thank you very much for this.

Original thread: https://forums.rpgmakerweb.com/threads/counting-actors-deaths.94450/