Average Damage to All Enemies equal to a Variable

Background:
I have a skill that damages all enemies, and the end state would be to use the average of the damage dealt to the enemy party to recover the actor's party via a common event.

Example:
Let us say that two enemies from a battle received damage from the skills using the following numbers for simplicity's sake: enemy 1 received 1,500 damage; enemy 2 received 1,000 damage. Then, using the average of 1,250 damage, the party would receive 1,250 in healing.

I know this can be done by setting a variable equal to a script (average damage to the enemy party), but I do not know how to write the said script. Community assistance is appreciated.
you need to take a step back, because that is extremely difficult to do without a plugin.
and then the entire structure will depend on which plugin and what options you have.

so first question is what plugins are you already using? some of them might already be the ones to support such options.
you need to take a step back, because that is extremely difficult to do without a plugin.
and then the entire structure will depend on which plugin and what options you have.

so first question is what plugins are you already using? some of them might already be the ones to support such options.
I did not realize the difficulty level of the ask and figured there was probably a plugin option. Though I did not know which plugin option to look into off the top of my head. The majority of my plugins are from Yanfly, but the assumed possibilities are listed below:

YEP_BattleCore
YEP_ActionSequence1, 2, & 3
YEP_BuffsandStatesCore
YEP_DamageCore
YEP_TargetCore
YEP_SelectionControl

I can screenshot my list if needed.
Background:
I have a skill that damages all enemies, and the end state would be to use the average of the damage dealt to the enemy party to recover the actor's party via a common event.

Example:
Let us say that two enemies from a battle received damage from the skills using the following numbers for simplicity's sake: enemy 1 received 1,500 damage; enemy 2 received 1,000 damage. Then, using the average of 1,250 damage, the party would receive 1,250 in healing.

I know this can be done by setting a variable equal to a script (average damage to the enemy party), but I do not know how to write the said script. Community assistance is appreciated.
Do you want the entire party to receive 1,250 in healing spread out?
(As in, if you have 4 party members, each member would recover 312 HP, since 1250/4=312.5)

Or do you want each member to recover 1,250 HP each?
Using the YEP plugins, I think you can accomplish this with a <Custom Execution> tag on the skill:

JavaScript:
<custom execution>
user._totalDamage = user._totalDamage || 0;
user._totalDamage += value;
if (user._targetsHit === targets.length - 1) {
  let healing = Math.floor(user._totalDamage / targets.length);
  $gameParty.aliveMembers().forEach(member => member.gainHp(healing));
  delete user._totalDamage;
  delete user._targetsHit;
} else {
  user._targetsHit++;
}
</custom execution>
Without any plugins, you can achieve that using 2 Common Events, 4 variables, and (as inconvenient as it is) put 2 event pages in each Troop battle event section. For example:

Common Event #1 will be called "My Skill - Turn"
Common Event #2 will be called "My Skill - Use"

Variables will be numbered 1 to 4.
(You can change the variables in case 1~4 are already taken in your project, I'll let you know where in underlined text)

In the Troops battle event section, create 2 pages. Both of them should have "Span" set to "Turn", and both of them should have a single event: The Common Event "My Skill - Turn".
The only difference between the two pages is the "Conditions" part.
One page's condition should only have "Turn End" ticked.
The other page's condition should only have "Turn" ticked, with both numbers set to 0.

The skill itself should obviously be set to hit "All Enemies", and in the "Effects" section (on the center-right), add a Common Event effect (located under the "Other" tab). Choose the "My Skill - Use" event.

In "My Skill - Turn", the only event you should have is the Script event. In it, paste the following:
JavaScript:
var x = 0;
var y = $gameTroop._enemies.length;

$gameTroop._enemies.forEach(e => {
    if(e.isDeathStateAffected()){y--}
    x += e.hp;
});

$gameVariables.setValue(1, y);
$gameVariables.setValue(2, x);
If Variables 1 and 2 are taken in your project, change 1 and 2 to any other Var ID in the last two lines.

In "My Skill - Use", you should have the following events:
1. A Script event, with the following code:
JavaScript:
var x = 0;
$gameTroop._enemies.forEach(e => {
    x += e.hp;
});
$gameVariables.setValue(3, x);

var z = $gameVariables.value(2) - $gameVariables.value(3);
z = Math.round(z / $gameVariables.value(1));
$gameVariables.setValue(4, z);
If Variables 1~4 are taken in your project, change them to any other Var ID in the last four lines wherever it says "$gameVariables".

2. A "Change HP" event, which affects the entire party, and the Operand is set to Variable #4.
3. At this point, you can add your own events, for example: A recovery sound and a message that says something like "The party recovered \V[4] HP!"
4. Another Script event, with the following code:
JavaScript:
var y = $gameTroop._enemies.length;

$gameTroop._enemies.forEach(e => {
    if(e.isDeathStateAffected()){y--}
});
$gameVariables.setValue(1, y);
$gameVariables.setValue(2, $gameVariables.value(3));
$gameVariables.setValue(4, 0);
If Variables 1, 2, and 4 are taken in your project, change them to any other Var ID in the last 3 lines.

Screenshots of how everything should look:
Spoiler: Screenshots
The skill itself:
1.png


First battle event page:
2.png


Second battle event page:
3.png


First Common Event:
4.png


Second Common Event:
5.png

And that's it, it should work now. I haven't tested it TOO extensively, so there might be some bugs. Let me know if you find any issues or if something isn't clear 🙂

Sample project via MediaFire
Without any plugins, you can achieve that using 2 Common Events, 4 variables, and (as inconvenient as it is) put 2 event pages in each Troop battle event section. For example:

Common Event #1 will be called "My Skill - Turn"
Common Event #2 will be called "My Skill - Use"

Variables will be numbered 1 to 4.
(You can change the variables in case 1~4 are already taken in your project, I'll let you know where in underlined text)

In the Troops battle event section, create 2 pages. Both of them should have "Span" set to "Turn", and both of them should have a single event: The Common Event "My Skill - Turn".
The only difference between the two pages is the "Conditions" part.
One page's condition should only have "Turn End" ticked.
The other page's condition should only have "Turn" ticked, with both numbers set to 0.

The skill itself should obviously be set to hit "All Enemies", and in the "Effects" section (on the center-right), add a Common Event effect (located under the "Other" tab). Choose the "My Skill - Use" event.

In "My Skill - Turn", the only event you should have is the Script event. In it, paste the following:
JavaScript:
var x = 0;
var y = $gameTroop._enemies.length;

$gameTroop._enemies.forEach(e => {
    if(e.isDeathStateAffected()){y--}
    x += e.hp;
});

$gameVariables.setValue(1, y);
$gameVariables.setValue(2, x);
If Variables 1 and 2 are taken in your project, change 1 and 2 to any other Var ID in the last two lines.

In "My Skill - Use", you should have the following events:
1. A Script event, with the following code:
JavaScript:
var x = 0;
$gameTroop._enemies.forEach(e => {
    x += e.hp;
});
$gameVariables.setValue(3, x);

var z = $gameVariables.value(2) - $gameVariables.value(3);
z = Math.round(z / $gameVariables.value(1));
$gameVariables.setValue(4, z);
If Variables 1~4 are taken in your project, change them to any other Var ID in the last four lines wherever it says "$gameVariables".

2. A "Change HP" event, which affects the entire party, and the Operand is set to Variable #4.
3. At this point, you can add your own events, for example: A recovery sound and a message that says something like "The party recovered \V[4] HP!"
4. Another Script event, with the following code:
JavaScript:
var y = $gameTroop._enemies.length;

$gameTroop._enemies.forEach(e => {
    if(e.isDeathStateAffected()){y--}
});
$gameVariables.setValue(1, y);
$gameVariables.setValue(2, $gameVariables.value(3));
$gameVariables.setValue(4, 0);
If Variables 1, 2, and 4 are taken in your project, change them to any other Var ID in the last 3 lines.

Screenshots of how everything should look:
Spoiler: Screenshots
The skill itself:
View attachment 246665

First battle event page:
View attachment 246666

Second battle event page:
View attachment 246667

First Common Event:
View attachment 246668

Second Common Event:
View attachment 246669

And that's it, it should work now. I haven't tested it TOO extensively, so there might be some bugs. Let me know if you find any issues or if something isn't clear 🙂

Sample project via MediaFire
The main problem I can see with this is that it'll be thrown off if enemies heal themselves with their action that turn, or end up doing extra healing if a character does a counterattack, or an enemy has a negative HP regen state.
Do you want the entire party to receive 1,250 in healing spread out?
(As in, if you have 4 party members, each member would recover 312 HP, since 1250/4=312.5)

Or do you want each member to recover 1,250 HP each?
The desired effect is for each party member to receive the example 1,250 HP. I will give this a shot. Thank you!
The main problem I can see with this is that it'll be thrown off if enemies heal themselves with their action that turn, or end up doing extra healing if a character does a counterattack, or an enemy has a negative HP regen state.
I knew I could do it with a common event; it seemed like a lot of work, and I only wanted to go that route as a last resort. I believe in working smarter, not harder 😉
The main problem I can see with this is that it'll be thrown off if enemies heal themselves with their action that turn, or end up doing extra healing if a character does a counterattack, or an enemy has a negative HP regen state.
Yup, I overlooked that :S
The issue happens when a confused/fascinated enemy attacks their ally that turn, too.

The main problem is that the program doesn't give us a way (that I know of) to run events right before a skill is used; only after. If anyone knows a way, it'll be a great help 🙂

In other words, without using plugins, I dunno if there's a way to check enemies' HP right before using the skill. You can check at the beginning of the turn, but like Trihan said... if an enemy's HP changes that turn (via healing or attacking an ally), my method is rendered useless lol

I'll keep trying to find a way, but I can't promise much since my RPG Maker JS coding knowledge is mediocre at best xD
Worst case, I'm sure there's a plugin or two out there that can achieve the desired effect (Such as YEP plugins, as mentioned above)
Yup, I overlooked that :S
The issue happens when a confused/fascinated enemy attacks their ally that turn, too.

The main problem is that the program doesn't give us a way (that I know of) to run events right before a skill is used; only after. If anyone knows a way, it'll be a great help 🙂

In other words, without using plugins, I dunno if there's a way to check enemies' HP right before using the skill. You can check at the beginning of the turn, but like Trihan said... if an enemy's HP changes that turn (via healing or attacking an ally), my method is rendered useless lol

I'll keep trying to find a way, but I can't promise much since my RPG Maker JS coding knowledge is mediocre at best xD
Worst case, I'm sure there's a plugin or two out there that can achieve the desired effect (Such as YEP plugins, as mentioned above)
Unfortunately yeah, the only way to accomplish that is using a plugin. There is no way natively to run an event before using a skill.
In other words, without using plugins, I dunno if there's a way to check enemies' HP right before using the skill.
And, comparing the two examples, trying to do it with events requires more code than simply using the Skills Core notetag (plus multiple events).

So unless there's some specific reason to avoid using a plugin (which I can't see), aside from the thought exercise of doing it through events it's just not necessary.
Yup, I overlooked that :S
The issue happens when a confused/fascinated enemy attacks their ally that turn, too.

The main problem is that the program doesn't give us a way (that I know of) to run events right before a skill is used; only after. If anyone knows a way, it'll be a great help 🙂

In other words, without using plugins, I dunno if there's a way to check enemies' HP right before using the skill. You can check at the beginning of the turn, but like Trihan said... if an enemy's HP changes that turn (via healing or attacking an ally), my method is rendered useless lol

I'll keep trying to find a way, but I can't promise much since my RPG Maker JS coding knowledge is mediocre at best xD
Worst case, I'm sure there's a plugin or two out there that can achieve the desired effect (Such as YEP plugins, as mentioned above)
Did you see my post above where I told you specifically how to do this with YEP plugins and the exact code for it?
Edit: never mind, I get it.
Did you see my post above where I told you specifically how to do this with YEP plugins and the exact code for it?
Ah, never mind. I was trying to do too many things at once. I will reevaluate this when I have more time.
Did you see my post above where I told you specifically how to do this with YEP plugins and the exact code for it?
Thank you for the code. Unfortunately, it is not healing my party. Code below:

JavaScript:
<custom execution>
user._totalDamage = user._totalDamage || 0;
user._totalDamage += value;
if (user._targetsHit === targets.length - 1) {
  let healing = Math.floor(user._totalDamage / targets.length);
  $gameParty.aliveMembers().forEach(member => member.gainHp(healing));
  delete user._totalDamage;
  delete user._targetsHit;
} else {
  user._targetsHit++;
}
</custom execution>

<setup action>

display action

immortal: targets, true

perform start

wait for movement

cast animation

wait for animation

</setup action>

<target action>

jump user: 200, 20

move user: target, front center, 20

wait for movement

motion swing: user

animation 181: target

wait for animation

action effect

animation 43: target

wait for animation

</target Action>

<Follow Action>

remove state 17: existing actors

</Follow Action>
Thank you for the code. Unfortunately, it is not healing my party. Code below:

JavaScript:
<custom execution>
user._totalDamage = user._totalDamage || 0;
user._totalDamage += value;
if (user._targetsHit === targets.length - 1) {
  let healing = Math.floor(user._totalDamage / targets.length);
  $gameParty.aliveMembers().forEach(member => member.gainHp(healing));
  delete user._totalDamage;
  delete user._targetsHit;
} else {
  user._targetsHit++;
}
</custom execution>

<setup action>

display action

immortal: targets, true

perform start

wait for movement

cast animation

wait for animation

</setup action>

<target action>

jump user: 200, 20

move user: target, front center, 20

wait for movement

motion swing: user

animation 181: target

wait for animation

action effect

animation 43: target

wait for animation

</target Action>

<Follow Action>

remove state 17: existing actors

</Follow Action>
What *is* it doing, if anything?
What *is* it doing, if anything?
It damages a single target as expected and plays the animations, but does not heal the party.

Edit:
Console screen-shot added.
Ah, "targets" is invalid for the set of targets of the skill. Try changing that to

BattleManager._targets
@Trihan - you may need another tag (as well?): <custom execution> appears to run once, before the action is invoked on any target, and does not offer a value variable.

Also, the core script BattleManager.updateAction shifts the next target out of BattleManager._targets prior to invoke/apply, so the current target is not present in _targets during invocation. I haven't checked to see if/how Yanfly's stuff changes that.

I'm thinking something like this may work (untested):
Spoiler: tags
JavaScript:
<Post-Damage Eval>
// --- do once per target --- //
// count hits (lazy init)
user._hitCount || (user._hitCount = 0);
++user._hitCount;
// count damage (lazy init)
user._totalDamage || (user._totalDamage = 0);
user._totalDamage += value;
</Post-Damage Eval>

<After Eval>
// --- do once per action --- //
// heal only if hit for some damage
if (user._hitCount && user._totalDamage) {
  // get average damage
  const healing = Math.floor(user._totalDamage / user._hitCount);
  // heal all living friends of user
  for (const m of user.friendsUnit().aliveMembers())
    m.gainHp(healing);
}
// clean up
delete user._totalDamage;
delete user._hitCount;
</After Eval>
[Edit: corrected 2nd </Post-Damage Eval> to </After Eval>.]
[Edit 2: it seems this has some problems, see post #29.]

Original thread: https://forums.rpgmakerweb.com/threads/average-damage-to-all-enemies-equal-to-a-variable.153085/