MV YEP_BuffsStatesCore - Auto-Potion when ally's at low hp

Hi !
Working on my project, I tried to have a character with a passive that makes him use a potion on an ally when they're at low hp, like 20% max HP, at the end of the turn.

I did the skill from YEP's Auto Potion which works as intended as a skill itself. Now I searched a bit for the "ally at 20% hp" thing on the forum but only found "lowest hp ally" which is different.

I tried to tweak this code I found here : https://forums.rpgmakerweb.com/index.php?threads/targetting-lowest-hp_rate-party-member.153664/

Code:
<Custom Turn End Effect>
var lowestMember = $gameParty.aliveMembers()[0];
for (var i = 1; i < $gameParty.aliveMembers().length; ++i) {
  var member = $gameParty.aliveMembers()[i];
  if (member.hpRate() < lowestMember.hpRate()) {
    lowestMember = member;
  }
}

And added a force action at the end to use the item. This works but since I'm not good with code and all that I don't know how, or even if it's possible, to change it to use the skill only if the ally is under 20% max HP.
Does anybody have an idea or a workaround to this ?
It's hard to give you specific instructions, since you didn't actually include your entire code.

But basically, above the force action you would just put
Code:
if (lowestMember.hpRate()<=.2)
Alright so I added your code, the problem is the target now :

Code:
  <Custom Turn End Effect>
var lowestMember = $gameParty.aliveMembers()[0];
for (var i = 1; i < $gameParty.aliveMembers().length; ++i) {
  var member = $gameParty.aliveMembers()[i];
  if (member.hpRate() < lowestMember.hpRate()) {
    lowestMember = member;
  }
}
if (lowestMember.hpRate()<=.2) {
BattleManager.queueForceAction(user, 113, -1);
}
  </Custom Turn End Effect>

Whether your line is here or not is not the issue tho, the force action cannot target a specific ally so I need to find a way to target the correct actor with the skill :distrust:
The skill is currently set to target one ally so it must target the first position member as default, I'll have to find something.
Whether your line is here or not is not the issue tho, the force action cannot target a specific ally
Yes it can - where did you get your reference for queueForceAction()?

You'd simply do
Code:
BattleManager.queueForceAction(user, 113, lowestMember);

As a side note, if you're only doing one thing after a conditional (if), you don't need to put braces around it.
Yes it can - where did you get your reference for queueForceAction()?

You'd simply do
Code:
BattleManager.queueForceAction(user, 113, lowestMember);

As a side note, if you're only doing one thing after a conditional (if), you don't need to put braces around it.
I don't remember the exact thread I found it on, I see it sometimes and remember the "target" cannot work aside from -1 being random target, -2 last target, and 0 and so on is for index (enemy or party member)

So just the (if) and then doing the conditional on the next line ? Do I need to put a ";" too ?

EDIT : The force action with the "lowestMember" worked, thanks ! Don't know why previous attempts on other things didn't work but for this it did 😅
You don't strictly need to, per JavaScript syntax, but it's recommended practice so that you help yourself keep track of what's a condition or a switch case versus a code statement. So:
Code:
if (lowestMember.hpRate()<=.2)
    BattleManager.queueForceAction(user, 113, lowestMember);

and 0 and so on is for index (enemy or party member)
Isn't that what a specific ally/enemy means?

From Yanfly's Battle Engine Core, which is where this function comes from:
Code:
    if (target === undefined) {
      var targetIndex = 0;
// If you don't supply the argument, it will be the first enemy/actor

    } else if (typeof target === 'number') {
      var targetIndex = target;
// You can type a number and it will be treated like the index in the default force action script calls. -2 is the last targeted index,
// -1 is a random target, any other value is that index

    } else {
      var targetIndex = target.index();
// You can supply an actor/enemy and it will automatically get their index in the appropriate unit
So for those wanting to use the same kind of thing here is the full code for auto-potion ONE ally when he's below a certain HP threshold :

Needs YEP_BuffsStatesCore and YEP_SkillCore to work :
Put this code on a state for the actor that will use the item :
Code:
  <Custom Turn End Effect>
var lowestMember = $gameParty.aliveMembers()[0];
for (var i = 1; i < $gameParty.aliveMembers().length; ++i) {
  var member = $gameParty.aliveMembers()[i];
  if (member.hpRate() < lowestMember.hpRate()) {
    lowestMember = member;
  }
}
if (lowestMember.hpRate()<=HPRATE)
BattleManager.queueForceAction(user, POTIONSKILL, lowestMember);
  </Custom Turn End Effect>

Where "HPRATE" is a decimal representing the actor's hp rate (like 20% hp is 0.2 in my case), and "POTIONSKILL" is the number of the skill that will use the item
The skill to auto use an item have been done this way, using YEP's "Auto-potion" trick in his site :

Code:
<Custom Requirement>
// Set the original condition to false.
value = false;
// If the user is an actor...
if (user.isActor()) {
  // Create an array of valid potions the counter can use.
  var potions = [];
  // Add the ID's of the potions that can be used for Auto-Potion.
  potions.push(POTIONID1, POTIONID2, POTIONID3, etc);
  // Loop through each of the potion ID's.
  for (var i = 0; i < potions.length; ++i) {
    // Get the current ID.
    var id = potions[i];
    // If the party has this potion in its item bag...
    if ($gameParty.numItems($dataItems[id]) > 0) {
      // ...then set the value to true.
      value = true;
      // ...and break the loop.
      break;
    }
  }
// If the user isn't an actor...
} else {
  // ...then the condition is automatically fulfilled.
 value = true;
}
</Custom Requirement>

<After Eval>
var potions = [];
potions.push(POTIONID1, POTIONID2, POTIONID3, etc);
for (var i = 0; i < potions.length; ++i) {
  var id = potions[i];
if ($gameParty.numItems($dataItems[id]) > ($gameVariables.value(LIMITVAR))) {
    var item = $dataItems[id];
    if (user.isActor()) {
      $gameParty.loseItem(item, 1);
    }
    this.setItem(id);
    var text = '<CENTER>' + user.name() + ' uses \\i[' + item.iconIndex + ']' + item.name + ' !';
    BattleManager.addText(text);
    break;
  }
}
</After Eval>

<setup action>
display action
action effect
</setup action>

Where "POTIONID1, POTIONID2, POTIONID3, etc" is the item IDs that you want to use. The item will be picked in the order you write them. For instance if you have Potion at ID 0004 you want the actor to use before an Elixir at ID 0007, you'll write it
Code:
  potions.push(4, 7);

And "LIMITVAR" is a variable you use to determine the amount of item the actor can use with this skill. It's to avoid the auto potion to consume all the items. So if you want to still have 5 Potions for the player to use that won't be consumed automatically you put :

Code:
if ($gameParty.numItems($dataItems[id]) > ($gameVariables.value(5))) {

Else just put 0 instead of "($gameVariables.value(LIMITVAR))"

I think I didn't miss an info here, I'll edit it if needed 😅

Again, thanks to ATT_Turan on this one 🙏

Original thread: https://forums.rpgmakerweb.com/threads/yep_buffsstatescore-auto-potion-when-allys-at-low-hp.162353/