Yanfly Buffs & States Core: Knockout removal doesn't trigger Custom Remove Effect

Using: Yanfly's Buffs & States Core + Base Param Control plugins

I've tested the functionality around Custom Remove Effect on a state & it appears to not actually run this "lunatic" code if the state in question was removed by Knockout (or death removal). Any ideas on how to trigger this effect upon knockout?

I have State007 which increases defense by 1 when applied & decreases defense by 1 when removed. I also have logic that applies it back to a character upon revival if the leader of the party is still alive. This has the unfortunate consequence of stacking that defense "buff". I'm expecting the Knockout to trigger the decrease defense by 1 effect... just wondering if there is a flag or something that I am missing here or if this is just how this portion is supposed to work.

Thanks in advance.
What is the exact code you're using to make the effect work?
So, it might be unnecessarily complicated... but I was trying to build a framework to easily drop in other states like this in the future. Here is the setup:

1. Knockout state notetag:
<Custom Apply Effect>
$gameSwitches.setValue(7, true);
</Custom Apply Effect>

<Custom Remove Effect>
$gameSwitches.setValue(8, true);
</Custom Remove Effect>

2. State 007 notetag:
<Custom Apply Effect>
var bonus = $gameVariables.value(16);
user.addDef(bonus);
</Custom Apply Effect>

<Custom Remove Effect>
var bonus = $gameVariables.value(16);
user.minusDef(bonus);
</Custom Remove Effect>

3. Base Troop Event 001
Battle Event 1 (listening for the Moment): When switch 7 is ON, call Common Event 17.
Battle Event 2 (listening for the Moment): When switch 8 is ON, call Common Event 17.

4. Common Event 17 (called for various reasons but the logic is smart enough to point to this logic on switches 7 or 8 & switch them to OFF):
If leaderId is 1 (which it is), If that leader is knocked out then remove State 007 from the party; else, run this code:

for (var j = 0; j < $gameParty.members().length; j++) {
if ($gameParty.members()[j].isStateAffected(7)) {
//do nothing
}
else {
$gameParty.members()[j].addState(7);
}};

Hope that helps...

edit: resolved by making the following changes

1. Added Yanfly's State Categories extension for Buffs & States core.
2. Added the death removal bypass note to State 007's notetag.
3. Added a check in the CE's code snippet to remove State 007 if the character is affected by both Knockout state & State 007:

for (var j = 0; j < $gameParty.members().length; j++) {
if ($gameParty.members()[j].isStateAffected(7)) {
if ($gameParty.members()[j].isStateAffected(1)) {
$gameParty.members()[j].removeState(7);
};
}
else {
$gameParty.members()[j].addState(7);
}};

There was probably a way more efficient or simple way to do it... if any of you come up with one, I'd be happy to review it 🙂

Original thread: https://forums.rpgmakerweb.com/threads/yanfly-buffs-states-core-knockout-removal-doesnt-trigger-custom-remove-effect.88028/