How to check enemy HP to apply a state on them?

Okay. I have enemies, and I have a special state (its ID is 12) which supposed to apply every time whenever enemy's HP gets below 25%.
I have found two ways to do it:
1)By battle events. It works, but I can'y o figure out how to remove this state in case whenever an enemy restores its health from 25%;

2)By common event:
Code:
◆Change Enemy State:Entire Troop, - Wounded
◆Control Variables:#0001 var1_8 = 0
◆Loop
  ◆Control Variables:#0002 var2_9 = 0
  ◆Control Variables:#0003 var3_10 = 0
  ◆Script:if ($gameTroop.members()[$gameVariables.value(1)])
  :      :{ $gameVariables.setValue(2, $gameTroop.members()[$gameVariables.value(1)].mhp);
  :      :$gameVariables.setValue(3, $gameTroop.members()[$gameVariables.value(1)].hp); }
  ◆Control Variables:#0002 var2_9 /= 4
  ◆If:var3_10 < var2_9
    ◆If:var3_10 > 0
      ◆Script:$gameTroop.members()[$gameVariables.value(1)].addState(12)
      ◆
    :End
    ◆
  :End
  ◆Control Variables:#0001 var1_8 += 1
  ◆If:var1_8 ≥ 8
    ◆Break Loop
    ◆
  :End
  ◆
:Repeat Above
Its supposed to trigger by any damaging skill, but It doesn't work somehow.

I've tried to use Yanfly's Auto Passive States plugin, but it crashes whenever it used along with other plugins, like Moghunter's Battle Hud and Graphics Changer.
can you give a screenshot of the common event and tell us how it is called?
because from the description you gave there is something wrong with the calling of the common event, so that it never is executed (hint: there are no parallel processes on any screen, parallels only work on the map)

as for the first solution you would either have to check the troops every turn if they get above 25%, or have the skill that heals them correct the state.
Yes. There are screenshots

1650054506978.png

The common event calls by damaging skills like Attack.
1650054564332.png
You are definitely going about this the hard way. I think it would take less effort to make a Yanfly plugin work with the Mog stuff (many other people have) than to go about it this way (and give you more useful options, in the end) 😉

That being said, your common event is overly convoluted. Put one single script box in it:
Code:
for (var i=0; i<$gameTroop.aliveMembers().length; i++)
{
    if ($gameTroop.aliveMembers()[i].hp<=$gameTroop.aliveMembers()[i].mhp/4)
        $gameTroop.aliveMembers()[i].addState(12);
    else
        $gameTroop.aliveMembers()[i].removeState(12);
}
Hello @Morizel. 👋

Yanfly's Autopassive States plugin appears to work without crashes or bugs with Moghunter's Battle HUD plugin when using the <Passive Condition: HP Below 25%> notetag on an enemy passive state.
Spoiler: MOG_BattleHud & YEP_AutoPassiveStates New Project Test
  1. Started a new project with no plugins but the following installed in order: MOG_BattleHud, YEP_CoreEngine and YEP_AutoPassiveStates.
  2. Created the state named, well, Special State with ID#0012. Added <Passive Condition: HP Below 25%> to its notetag.
  3. In the plugin parameters of YEP_AutopassiveStates, set Enemy Passive List to ["12"].
  4. Playtested battles with enemies under various scenarios e.g. enemy damaged with Attack skill, enemy Max HP set below 25% at Turn 0, enemy poisoned, enemy with HP Regeneration, enemy healing allies, etc.
  5. The state was applied as expected in every scenario. I didn't experience crashes or error logs during playtesting.
If you're not using YEP Autopassive States due to compatibility issues with other plugins, you can try the custom plugin attached below. Just install it at or near the bottom of your plugin list. I've succesfully tested it in a new project with no other plugins except for MOG_BattleHud installed at the top of the plugin list.
Spoiler: PLUGIN CODE
JavaScript:
{
    const _specialState_gainHp = Game_Battler.prototype.gainHp;
    Game_Battler.prototype.gainHp = function(value) {
        _specialState_gainHp.call(this, value);
        if ($gameParty.inBattle() && this.isEnemy()) {
            const specialStateID = 12;                                        // special state ID
            if (this.isStateAffected(specialStateID)) {
                if (this.hpRate() >= 0.25) this.removeState(specialStateID);
            } else {
                if (this.hpRate() < 0.25) this.addState(specialStateID);
            }           
        }
    };
}
Spoiler: PLUGIN FEATURES
  • Plug and play. No need to call a Common Event with every damaging or recovery skill. No need to call Battle Events with every troop.
  • Will work with all kinds of HP damage and recovery including passive ones e.g. damage from Poison state and healing from HP Regeneration.
  • Will work with troop Battle Events including ones that reduce enemy Max HP below 25% at battle start i.e. Turn 0.
  • Hopefully compatible with other plugins.
Spoiler: PLUGIN CAVEATS
  • Only works during battle and affects enemies only. Player characters are not affected.
  • Per your OP, the special state is supposed to be applied BELOW 25% Max HP, not AT OR BELOW 25% Max HP. Put in another way, the state will NOT be applied at EXACTLY 25% Max HP.
  • Although this plugin has been designed for maximum compatibility with other plugins, I haven't checked possible incompatibilities with every plugin. This is the first time I've tried using any of Moghunter's plugins so I'm unfamiliar with possible incompatibilities when using his plugins.
  • If you have a skill that somehow heals then damages an enemy target (all within one action of the same skill), the plugin as it is will not work properly. I disabled this feature in the code below because it's unlikely you'll create something like it. If for some reason you still want to create this weird skill or something similar uncomment the following code in the attached plugin:
Spoiler: Correction Code for Weird Skills
JavaScript:
{
    const _specialState_isStateAddable = Game_Battler.prototype.isStateAddable;
    Game_Battler.prototype.isStateAddable = function(stateId) {
        const specialStateID = 12;                                            // special state ID
        if (stateId === specialStateID) {
            return (this.isEnemy() && this.isAlive() &&
                $dataStates[stateId] &&
                !this.isStateResist(stateId) &&
                !this.isStateRestrict(stateId));
        }
        return _specialState_isStateAddable.call(this, stateId);       
    };
}
Spoiler: Correction Code Testing
I successfully tested the above correction code through the Damage Formula with a damaging skill that initially heals then subsequently damages the target e.g Damage Type: HP Damage, Formula: b.gainHp(10); 10. The user was an enemy with the skill and the target was another enemy with Max HP just below 25%.
Spoiler: Sidenote
@Morizel Thank you for introducing me to Moghunter's Battle HUD plugin through this thread. I never touched his plugins before due to their purported conflicts with Yanfly's plugins which I use primarily.
I can now understand why if given the mutually exclusive choice between Moghunter's or Yanfly's plugins, you'll choose the former: his Battle HUD plugin alone is worth it because it's gorgeous! 😍
Yanfly's Autopassive States plugin appears to work without crashes or bugs with Moghunter's Battle HUD plugin when using the <Passive Condition: HP Below 25%> notetag on an enemy passive state.
Thank you. Unfortunately, I have bad experience with Yanfly's Autopassive States plugin. Usually I get crash with "maximum call stack size exceeded" error. That time it was called by player's death in combat. The log has related to Mog's BattleHUD, Graphics Changer and Yanfly's Battle Core Engine. Thus I've decided to be careful this one.
I'll try yours.
That being said, your common event is overly convoluted. Put one single script box in it:
"i" is just a variable's ID? So, if I will use variable 1 instead of i, then it should looks like this?
Code:
for (var 1=0; i<$gameTroop.aliveMembers().length; 1++)
{
    if ($gameTroop.aliveMembers()[1].hp<=$gameTroop.aliveMembers()[1].mhp/4)
        $gameTroop.aliveMembers()[1].addState(12);
    else
        $gameTroop.aliveMembers()[1].removeState(12);
}
The script works good. The only problem I have found is that its working only whenever enemy attacks. That's bothering me a little. Maybe call for it at end of a turn?
Actor doesn't trigger it. This is fine.
The script works good. The only problem I have found is that its working only whenever enemy attacks. That's bothering me a little. Maybe call for it at end of a turn?
Actor doesn't trigger it. This is fine.
Hello @Morizel. If you're concerned about the visual timing of when you see the states applied or removed you can use the plugin I made. (It's even named after you. 😊)
The state will be applied or removed the moment your HP threshold is met. You should see for yourself the visual results when you use skills that hit more than once or skills that hit all enemies.
It will even work on poison or HP regeneration without using any battle events.

In case you intend to use setHp() in your damage formulas, script calls, etc. you should use the revised plugin attached below. Otherwise you should use the first plugin instead and ignore this one.
I agree with ATT_Turan that the simplest option here would be to use a plugin like YEP Auto Passive States.
Unfortunately, I have bad experience with Yanfly's Autopassive States plugin. Usually I get crash with "maximum call stack size exceeded" error. That time it was called by player's death in combat.
That sounds like a recursive loop, possibly a problem with your notetags. If you had a notetag to do something when the death state (state 1) is added, maybe that was responsible. Yanfly's plugins should also be loaded in the order specified on their site, i.e.

Original thread: https://forums.rpgmakerweb.com/threads/how-to-check-enemy-hp-to-apply-a-state-on-them.146796/