MV Trying to make a few variables increase when a character dies (part 2)

(made a few minor edits to try to be more clear)

Plugins that I'm using relevant to what I'm trying to code:

Yanfly's battle core
Auto passive states:
Buffs and states core
Common troop events
Hime's end phase triggers (though it doesn't seem to work or maybe it has to do with the code I put in).

Okay, so I had a bug once upon a time. Most of the people here helped me get it working, then I discovered there was stuff missed the first time around and here we are. So I'm going to try explaining again and stating where I think the issue is.

So my game is based around character moods. I'm trying to implement a mechanic where if one character dies in battle, the variable for the other 2 characters' "sadness" moods increase, and then a common troop event plays where the character reacts to it.This is the code I have currently. I have an idea on where the problem is coming from I think, but I'm not sure what I need to change to fix it. I've bolded the areas I think are possibly suspect, and I'll give an outline on what I'm trying to make the code do, lol. Each of the three main characters has a similar hidden passive state with slightly different variables.

<Custom Battle Effect>

if (user.isDead() {
user.sadHit = true;
}
else {
user.sadHit = false;
}

//this is setting the flag so that if the user of the state is already
dead, it doesn't automatically trigger it.//

</Custom Battle Effect>

<Custom Respond Effect>

//If target is dead (from enemy attack), and target doesn't have sadHit, and target is actor 1//

if (target.isDead() && !target.sadHit && target.actorId()==1)
{

// turn on sadHit, variables 77 and 83 are sadness variables for actors 2 and 3. Switch 101 turns on the common troop event where the characters cry out at the loss of their companion.//

target.sadHit = true;
$gameVariables.setValue(77,
$gameVariables.value(77) + 20);
$gameVariables.setValue(83,
$gameVariables.value(83) + 20);
$gameSwitches.setValue(101, true);
}
else if (!user.isDead() && target.sadHit)
{
target.sadHit = false;
$gameSwitches.setValue(101, false);
}


</Custom Respond Effect>

<Custom Regenerate Effect>

if (user.isDead() && !user.sadHit && target.actorId()==1)
{
user.sadHit = true;
$gameVariables.setValue(77,
$gameVariables.value(77) + 20);
$gameVariables.setValue(83,
$gameVariables.value(83) + 20);
$gameSwitches.setValue(101, true);
}
else if (!user.isDead() || user.sadHit)
{
user.sadHit = false;
$gameSwitches.setValue(101, false);
}

</Custom Regenerate Effect>

So what I want the code to do is each time a character is killed in battle, the other two characters get the sadness variable to go up, then it plays the common troop event where they're all "NO WHYYYY" or what have you.

The respond effect portion is working great as far as I can tell. They die, they get sad, event triggers, all good.
The Regenerate effect is there to make sure it covers for things like, if a character dies from poison, or a character sacrifices themself, or dies from a global attack since it's not targeted etc.

But the bug(s) are, 1) If a character dies before that regenerate effect is triggered and the character is still dead the next battle, it will trigger next regen round on the battle after the current. In fact, as long as the character stays dead, it triggers once every battle afterwards. Bringing the character to life does prevent the regen from triggering, but otherwise the characters are mourning their dead cohort once every battle. I tried to set the sadHit object at the beginning of the battle to prevent this, but the Regenerate effect kicks in no matter what.

2) If the last enemy in a troop is killed at the same time a character dies, the battle ends before the mouring can trigger. This isn't that big of deal, except it leads to more instances of bug 1. I tried fixing this with a Hime plugin (end phase triggers I think) but I didn't see any change.

As a note, bug 1 only happens if the character is dead when the regen trigger hits. If you have a character die from a self destruct ability, then heal them, then start a new battle, it doesn't trigger. So hope that helps. Let me know what I'm missing.

To summarize the challenge is getting the death reaction to trigger each time a character is killed in battle but not to trigger if they're already dead. Ideally I want it to also work where if a character is killed, revived and killed again, the animation variable and troop event triggers each time.
you may want to approach the idea differently. No idea how itll impact the rest of your game, but my thoughts are that if all you need to do is make the variable incrementing and switch flipping happen when that actor dies, then itll be easier to target the things that must happen when they die instead of around it.

the 2 things that come to mind would be either moving the logic into an <custom apply effect> of your KO state so that if the target is actor 1, all that stuff happens, or adding a bit of extra code to the functions that get ran as a battler dies like Game_BattlerBase.addNewState so that if its your actor 1 being killed it happens

if you save this as a .js file and use it in your project as a plugin, youd be able to increment the variables any time that actor dies from any cause.
Code:
var _MCDeath = Game_BattlerBase.prototype.addNewState;
Game_BattlerBase.prototype.addNewState = function(stateId) {
  _MCDeath.call(this, stateId);
  if (stateId === this.deathStateId() && this == $gameActors.actor(1)) {
      $gameVariables.setValue(77, $gameVariables.value(77) + 20);
      $gameVariables.setValue(83, $gameVariables.value(83) + 20);
      $gameSwitches.setValue(101, true);
  }
}
then you can add in more specific checks you'd probably want (like not incrementing variable 77 unless actor 2 is in the party as an example), and change the common event to flip off its trigger switch if you havent already.

that should take care of issue 1. Not sure about issue 2 as i dont know how your common event is being called and im not going to look into plugins im not familiar with tonight. i dont think parallel common events run in battles, but if you have something that already works in battle, then adding a parallel common event on the same switch would make the "oh no!" messages play after battle if it couldnt be played during it. you'd just need to make sure both flip the switch off at the end
Okay so question if I try it this way:

Does it make a difference if I have this effect for all three characters? The code I put here is currently if actor 1 dies, but I actually have duplicate and similar hidden states for all three characters, each with different sadness variables and different switches for similar common events.

As far as the common event goes, I'm using common troop events plugin ( I think that's the name)
Does it make a difference if I have this effect for all three characters? The code I put here is currently if actor 1 dies, but I actually have duplicate and similar hidden states for all three characters, each with different sadness variables and different switches for similar common events.
you'd just have to add extra conditionals alongside it to affect the other actors.
Code:
var _MCDeath = Game_BattlerBase.prototype.addNewState;
Game_BattlerBase.prototype.addNewState = function(stateId) {
  _MCDeath.call(this, stateId);
  if (stateId === this.deathStateId()) {
    if (this == $gameActors.actor(1)) {
      $gameVariables.setValue(77, $gameVariables.value(77) + 20);
      $gameVariables.setValue(83, $gameVariables.value(83) + 20);
      $gameSwitches.setValue(101, true);
    }
    else if(this == $gameActors.actor(2)) {
      $gameVariables.setValue(78, $gameVariables.value(78) + 15);
      $gameVariables.setValue(88, $gameVariables.value(88) + 15);
      $gameSwitches.setValue(102, true);
    }
  }
}
from there, youd just add blocks for each actor, increment their variables, and flip whatever switches are needed for their common events, and make sure each common event flips its own switch at the end. I dont imagine it would break what you currently have going
Okay so I'd just add the third else if block for actor three and I'm good then?

So this means after I create the plugin I'd remove all the code from the existing hidden states I already have, keep the passive states tag on each character page, and just reference the same state IDs I'm currently using?

Also unimportant side question: I've been learning you typically want to use let instead of var in JavaScript unless you're working with older code or if you need it to be global. For my own learning sake, are you using var for _MCDeath because it needs to be a global variable?

one more question: let's say I'm using state id 100, where would I put the number for the state in the plugin code, or would I?
(This is what I was talking about when I'm said I'm still trying to understand functions, lol)
So this means after I create the plugin I'd remove all the code from the existing hidden states I already have, keep the passive states tag on each character page, and just reference the same state IDs I'm currently using?
you wouldnt need the states at all, but id keep the code for them around just incase. you can remove the passives from the actors completely if they werent doing anything else

Also unimportant side question: I've been learning you typically want to use let instead of var in JavaScript unless you're working with older code or if you need it to be global. For my own learning sake, are you using var for _MCDeath because it needs to be a global variable?
var and let have different uses depending on how you intend on using the variable made. They both follow different rules in regards to scope, but in all practicality you can get away with using one over the other 99% of the time. unless I run into the need to use the other declaration keywords, i just use var. I dont use block declarations enough for it to make an impactful difference most of the time

That being said, in this case, you could actually use any of the declaration keywords in this case and it would still work. _MDCeath being assigned to the old function is done only once and being accessible to the one place where its ever used means var, let, and const all work.

one more question: let's say I'm using state id 100, where would I put the number for the state in the plugin code, or would I?
(This is what I was talking about when I'm said I'm still trying to understand functions, lol)
a bit confused on what you mean for this one. the only thing the plugin does is run that extra code if and only if the death state of an actor is being applied to them. You dont need to indicate any states within it. the death state's id is grabbed automatically, and it doesnt care for any others
Thank for for clarifying. I'm excited to get to my desk and test it out. I'll let you know how it goes.
are you using var for _MCDeath because it needs to be a global variable?
var does not create a global variable when used inside a function, which is everything you're doing (unless you're trying to write your own plugin).


I'll just put it out there - I don't see anything wrong with @Robro33's approach, but personally I would've thought it easier to just put this code inside a Custom Apply Effect notetag on your death state. Maybe I'm not thinking of something and there's a reason he didn't suggest doing it that way.

For that matter, as well as I understand what your goal is with this, I wouldn't bother doing anything with any notetags or passives or code at all.

I'd use Yanfly's Base Troop Events to put the same event into every troop; that event has a condition of an actor at <= 0 HP, and does the mourning cry and Control Variables and stuff. (that plugin is technically optional, you can just copy and paste the troop events into every troop)

Using Hime's End Phase Triggers will ensure that goes off. Simply that should be all you need, I don't see the need for any JavaScript here.
personally I would've thought it easier to just put this code inside a Custom Apply Effect notetag on your death state. Maybe I'm not thinking of something and there's a reason he didn't suggest doing it that way.
You have the right of it. I did mention it as the first way to go about it, but I wanted to provide a functional alternative just in case it didnt work out as expected. It was late and i had morning plans, so there was no promising a quick response if it didnt do the trick for whatever reason.

I'd use Yanfly's Base Troop Events to put the same event into every troop; that event has a condition of an actor at <= 0 HP, and does the mourning cry and Control Variables and stuff.
I did try to avoid troop events flipping the switch though. From the first post, it sounded like the call to end the battle if all enemies are dead goes off before before troop event page conditions are checked despite HIME's plugin sounding like it would change it.
You have the right of it. I did mention it as the first way to go about it
Sorry, I did miss that in the text above your code.

I did try to avoid troop events flipping the switch though. From the first post, it sounded like the call to end the battle if all enemies are dead goes off before before troop event page conditions are checked despite HIME's plugin sounding like it would change it.
If it's all correctly set up, that's exactly the point of Hime's End Phase Triggers. I think this would be most easily implemented by the OP via troop events. But maybe there's something I'm not thinking of/understanding about the situation.
Hey so wanted to update this and say the fix worked. I tried the plugin method and it seems to have done the trick wonderfully. It's kicking in end when it should so both problems are solved. Could have been because I'm using the Hime plugin but regardless.

I don't know if doing it as a common troop event would have worked better, but this appears to be working perfectly so not going to try to fix anything that isn't broken, lol.

I am really grateful for this. I was having such a headache with it. Thank you!
I don't know if doing it as a common troop event would have worked better, but this appears to be working perfectly so not going to try to fix anything that isn't broken, lol.
It's not that either method works "better" in a way that's perceptible to anyone playing the game - I was just observing that you could probably have done it originally in a way that didn't necessitate you trying to get into code at all, thus "easier" 🙂

Glad you got it working.
Thank you, I appreciate the help.

Original thread: https://forums.rpgmakerweb.com/threads/trying-to-make-a-few-variables-increase-when-a-character-dies-part-2.168596/