Check Ally States/HP

Is there any way to run a check that returns the number of other allies that meet a certain criteria (such as .isStateAffected() or .hp <= 50%, if that's a thing)?

I'm using Yanfly's Buffs&States Core to work with state counters, and am trying to, upon applying this State, increase the counters based on the number of allies (excluding the user) who either have under 50% HP, or who have a Passive State that only comes into effect while they have under 50% HP. It seems like doing some sort of call like the one above would be the best way to handle this, but if there's a better way, I'll give it a shot!
You might be able to do this with a troop event set to run at start of turn. However, it would not account for changes during that turn, so if you need that then my idea will not work.

Otherwise it will take code. I'll put this in the JavaScript Forum so you can better get help for this as you'll need the engine code to pull this off if my idea will not work.

Moved to JavaScript Support

You might be able to do this with a troop event set to run at start of turn. However, it would not account for changes during that turn, so if you need that then my idea will not work.

Otherwise it will take code. I'll put this in the JavaScript Forum so you can better get help for this as you'll need the engine code to pull this off if my idea will not work.

Moderator note: Moved to JavaScript Support

Ah, thanks! While ideally this would be able to track changes mid-turn, I haven't paid much attention to Troop Events and will start looking into what can be done with that.

In the meantime, if anybody knows how this might be done with code (I imagine it might have something to do with $gameParty?), let me know!
I haven't tested, but maybe using a Buffs & States notetag something like this would work?
Code:
<Custom Apply Effect>
var injured = 0;
var allies = $gameParty.battleMembers();
for (var n = 0; n < allies.length; n++) {
  var ally = allies[n];
  if (2 * ally.hp <= ally.mhp) injured++;
}
this.addStateCounter(42, injured);
</Custom Apply Effect>
Basically: when applying the state, check all present allies' HP percentage, tally the number of "injured" (ones with at most 50% HP), then add to the appropriate state's counter (replace the 42 as apt) based on the number of injured.
I haven't tested, but maybe using a Buffs & States notetag something like this would work?
Code:
<Custom Apply Effect>
var injured = 0;
var allies = $gameParty.battleMembers();
for (var n = 0; n < allies.length; n++) {
  var ally = allies[n];
  if (2 * ally.hp <= ally.mhp) injured++;
}
this.addStateCounter(42, injured);
</Custom Apply Effect>
Basically: when applying the state, check all present allies' HP percentage, tally the number of "injured" (ones with at most 50% HP), then add to the appropriate state's counter (replace the 42 as apt) based on the number of injured.

Ah, this worked almost perfectly! Due to how I have the State operate, I had to do a little tweak at the end (exchanging "
this.addStateCounter(42, injured);" with "user._countername += injured; user.setStateCounter(stateId, user._countername);" and having the counter subtract one if the user has less than half HP, so that it only scales with injured allies), but that's mostly since I'm using a plugin to determine State Counters and not whatever built-in functionality comes with RMMV.

Thanks for the help! Learned a lot of new language on this one that should come in handy later on. If anyone else wants to try something like this, it's about as straightforward as the code in caethyril's post.😤

Original thread: https://forums.rpgmakerweb.com/threads/check-ally-states-hp.92474/