Execute skill kills target if HP is lower then x%

Im sure I have seen this around before but I can't seem to find it or create it.
I want to have a skill that
a)Either deals massive bonus damage if target HP is lower then 20%
b)Instant kills target if lower then 20%
c)Checks if the target has a state (marked for death) applied before the instant kill works.

Can this be done with damage formulas?
Hey there,
I rarely work with Javascript so I might get this wrong, but it should work like this:

a) For conditional damage bonuses you can usually use this pattern:
(condition) ? (damage when fulfilled) : (damage when not fulfilled)
"Targets HP below 20%" could look like this for example:
(b.hp < b.mhp * 0.2) ? (120000) : (8000)

b) I'm not the biggest fan of using the damage formula for things not related to damage calculation. By default, skills that use this break the "Autobattle" trait (and maybe one or two similar plugins) as the damage formula would also be used to estimate skill effects on each target. It also causes each skill to show damage/healing done, even when the skill is not supposed to do actual damage.

For an alternative, you could take a look at Yanflys Skill Core plugin, which offers you a range of additional skill modifiers, including the option to code additional skill effects in the skills notebox:
<Post-Damage Eval>
...
</Post-Damage Eval>


Of course, using the damage formula box is less complicated and usually still fine to use if you prefer that. 🙂


Anyway:
Adding code to a damage formula can usually be done through this pattern:
(code); return (normal damage formula);

For killing a target that has little HP / a specific state applied (state 20 in this case), you could use these commands:
if (b.hp < b.mhp * 0.2) { b.addState(1); }
if (b.isStateAffected(20)) { b.addState(1); }



For more ideas or help, you could also look at the "RMMV Damage Formula - ideas and help" thread pinned in this subforum.
Hey there,
I rarely work with Javascript so I might get this wrong, but it should work like this:

a) For conditional damage bonuses you can usually use this pattern:
(condition) ? (damage when fulfilled) : (damage when not fulfilled)
"Targets HP below 20%" could look like this for example:
(b.hp < b.mhp * 0.2) ? (120000) : (8000)

b) I'm not the biggest fan of using the damage formula for things not related to damage calculation. By default, skills that use this break the "Autobattle" trait (and maybe one or two similar plugins) as the damage formula would also be used to estimate skill effects on each target. It also causes each skill to show damage/healing done, even when the skill is not supposed to do actual damage.

For an alternative, you could take a look at Yanflys Skill Core plugin, which offers you a range of additional skill modifiers, including the option to code additional skill effects in the skills notebox:
<Post-Damage Eval>
...
</Post-Damage Eval>


Of course, using the damage formula box is less complicated and usually still fine to use if you prefer that. 🙂


Anyway:
Adding code to a damage formula can usually be done through this pattern:
(code); return (normal damage formula);

For killing a target that has little HP / a specific state applied (state 20 in this case), you could use these commands:
if (b.hp < b.mhp * 0.2) { b.addState(1); }
if (b.isStateAffected(20)) { b.addState(1); }



For more ideas or help, you could also look at the "RMMV Damage Formula - ideas and help" thread pinned in this subforum.

a) Ah cool Thanks! I tried that but got it wrong, also I think it needs to be <= so that if enemy has 20% it still kills them.

b) Yes me either, the way my game works is with a smaller set of skills but has different effects based on interactions and states. This skill can deal bonus damage or death if enemy has another state. Im trying to do a sort of combo skill system.

c) I tried putting
<Pre-Damage Eval>
if (b.isStateAffected(16)) { b.addState(1); }
</Pre-Damage Eval>
in the skill note tag but it does not work. I also tried using the post-damage tag same result.
What did I do wrong?
in Yanfly's Skill Core plugins, "a" must be replaced with "user", and "b" must be replaced with "target". Besides, i personally think setting target's HP to 0 by using "target.setHp(0)" to kill it is better than just add state 1.
Haven't tried it out, but judging from the plugin you should be able to use either "a", "user" or "subject" for the attacker and "b" or "target" for the defender.

target.setHp(0) should work too. 🙂 (Edit: Corrected)
Not sure if I have a preference here though, if I don't miss anything the main difference would be that if a target is immune to death, this would actually set their HP to 0 instead of doing nothing.

---

I don't know if MV has a similar behaviour with its plugin system, but in previous makers changes to the script editor would only be considered in play-/battle-test mode if the project had been saved afterwards.

The pre- and post-damage effects seem to only get executed when the skill actually has a damage formula. If it hasn't, you could try to use the
<after eval> ... </after eval>
block instead and add a condition to make sure the skill actually hit:
if (b.result().isHit()) {
...
}

If you use the console during test play it should show an error message if an error occurred during the skill effect.

Apart from that, just to make sure, are you using any other plugins?
Haven't tried it out, but judging from the plugin you should be able to use either "a", "user" or "subject" for the attacker and "b" or "target" for the defender.

target.setHP(0) should work too. 🙂
Not sure if I have a preference here though, if I don't miss anything the main difference would be that if a target is immune to death, this would actually set their HP to 0 instead of doing nothing.

---

I don't know if MV has a similar behaviour with its plugin system, but in previous makers changes to the script editor would only be considered in play-/battle-test mode if the project had been saved afterwards.

The pre- and post-damage effects seem to only get executed when the skill actually has a damage formula. If it hasn't, you could try to use the
<after eval> ... </after eval>
block instead and add a condition to make sure the skill actually hit:
if (b.result().isHit()) {
...
}

If you use the console during test play it should show an error message if an error occurred during the skill effect.

Apart from that, just to make sure, are you using any other plugins?

Just when I think I sort of understand something... I dont have any clue at all.

I tried
<After Eval>
if (target.isStateAffected(16)) { target.addState(1); }
</After Eval>
<Pre-Damage Eval>
if (target.isStateAffected(16)) { target.addState(1); }
</Pre-Damage Eval>
Both do nothing. I tried using target.setHP(0) in the pre-damage eval portion, but I don't know how to make the code so I got errors when play testing.

I prefer also having it set the state so I can add enemies who resist it otherwise it would be a 2 skill combo to take down any boss.

Im play testing it by saving and opening my map so no issues there.

Ill be honest I have no idea what this means lol:
block instead and add a condition to make sure the skill actually hit:
if (b.result().isHit()) {"
It's "target.setHp(0)", not "setHP". Syntax errors are very common so we have to be very careful 🙂
Hi!! I think it is not that hard if you use some yanfly scripts, if you are willing to use them, I can help you in developing the skill you need. I just need some clarification:

You want the skill to (Option1) make massive damage to target if the target is below 20% maxHp or (Option2) insta kill the target IF the target is below 20% maxHp AND is "marked to dead" state?
It's "target.setHp(0)", not "setHP". Syntax errors are very common so we have to be very careful 🙂
Oh... Woops =)


@Icenick:
The condition was meant to be used like this:
<After Eval>
if (b.result().isHit()) {
if (target.isStateAffected(16)) { target.addState(1); }
}
</After Eval>

as this block is executed regardless whether the skill missed, was dodged or even could not be applied at all (e.g. target was already dead).
Though as long as your code does nothing, I'd not recommend making it any more complex before you figured out why. If that is still the case, the easiest way to start that I could think of would be replacing it with some nonsense that is bound to create an error and check the console if the Skill Core actually dumps a message (or if your block gets ignored completely).
As for that, I have never used the console either, but according to this thread you can press F8 to bring it up if you are running the game in the stand-alone client. 🙂

Edit: Alternatively, you should be able to use
$gameVariables.setValue(10, $dataSkills[11].customAfterEval);
in an event script command to set variable 10 to skill#11's "after eval" code. If the variable matches your code but nothing happens when using your skill, there might be something wrong with your code or some other plugin sabotages the Skill Cores execution of it.
It's "target.setHp(0)", not "setHP". Syntax errors are very common so we have to be very careful 🙂
Ahh I really need to find a good tutorial to help me learn all this stuff... I can't tell if im not writing it proper or just a capital letter lol.

Hi!! I think it is not that hard if you use some yanfly scripts, if you are willing to use them, I can help you in developing the skill you need. I just need some clarification:

You want the skill to (Option1) make massive damage to target if the target is below 20% maxHp or (Option2) insta kill the target IF the target is below 20% maxHp AND is "marked to dead" state?
I use a ton of Yanfly plugins. So yes im relying heavily on scripts just so I can learn the maker.
Actually I wanted to learn how to do both options. Im making different skills for a type of assassin skill set:
Skill 1- 2x damage (the multiplier is not really relevant but lets say 2x) if target is below 20% (with the exception of a few enemies)
Skill 2- Instant kill on enemies who have under 20% maxHP, otherwise normal damage.
Skill 3- Instant kill on enemies who have under 20% maxHP and have the "marked for death" state applied, otherwise normal damage.

Oh... Woops =)
@Icenick:
The condition was meant to be used like this:
<After Eval>
if (b.result().isHit()) {
if (target.isStateAffected(16)) { target.addState(1); }
}
</After Eval>

as this block is executed regardless whether the skill missed, was dodged or even could not be applied at all (e.g. target was already dead).
Though as long as your code does nothing, I'd not recommend making it any more complex before you figured out why. If that is still the case, the easiest way to start that I could think of would be replacing it with some nonsense that is bound to create an error and check the console if the Skill Core actually dumps a message (or if your block gets ignored completely).
As for that, I have never used the console either, but according to this thread you can press F8 to bring it up if you are running the game in the stand-alone client. 🙂

This is my code and it does not work:
<After Eval>
if (b.result().isHit()) {
if (target.isStateAffected(16)) { target.addState(1); }
}
</After Eval>

<formula effect>
if (b.isStateAffected(15)) b.addState(16)
</formula effect>

<formula effect>
if (b.isStateAffected(14)) b.addState(15)
</formula effect>

<formula effect>
b.addState(14)
</formula effect>

What happens is the skill applies state 14, then if you use the skill again it checks for 14 if target has 14 it removes 14 and adds 15, so on till the 3rd time state 16 is applied (Marked for Death). At this point I want the next application of this skill to check if 16 is applied and kill the target if the HP is lower then 20% maxHP.
Currently with this formula it does not even KO when state 16 is applied.
Im the following plugins in additon to Yanfly:
1) Overriding states http://himeworks.com/2015/11/overriding-states/ this just allows me to not have state 14-15-16 all on the target but takes the highest priority which is 16.
2) Formula Effects http://himeworks.com/2016/02/formula-effects-mv/ this just allows you to add the formula in notetag rather then formula bar. Much easier to sort and test through.

Edit: Alternatively, you should be able to use
$gameVariables.setValue(10, $dataSkills[11].customAfterEval);
in an event script command to set variable 10 to skill#11's "after eval" code. If the variable matches your code but nothing happens when using your skill, there might be something wrong with your code or some other plugin sabotages the Skill Cores execution of it.
I dont know what this means :S. What I did was create an event where I press enter to trigger $gameVariables.setValue(10, $dataSkills[11].customAfterEval); then start battle processing. It did not work.

Okay so I turned off Yanfly Battle core and your script worked, not the $gameVariables.setValue(10, $dataSkills[11].customAfterEval); . The
<After Eval>
if (b.result().isHit()) {
if (target.isStateAffected(16)) { target.addState(1); }
}
</After Eval>
However it instantly kills target because of my script where if state 15 then apply 16.
I found the solution which was simply using:
<Pre-Damage Eval>
if (b.result().isHit()) {
if (target.isStateAffected(16)) { target.addState(1); }
}
</Pre-Damage Eval>
This works exactly how I intended it, before damage it checks if it has state if it does apply the effects. However I still need to find a way to have it check if target maxHP is less then 20% and if it has the state.
Also I need to find out how to get this working with YEP Battle Core... Kind of need that plugin lol
Ok, I hope this is what you want. I have all of Yanfly Plug ins, and all of them are On (Including the Battle core, of course). So it should work for you.

For Skill1-
Code:
<Pre-Damage Eval>
if (b.result().isHit()) {
if (b.hp <= b.mhp * .20 && target._enemyId != 3) {
value = value * 2;
 }
}
</Pre-Damage Eval>
* This one will multiply the damage by 2 if the target has its hp on 20% or below its max. Also, you mentioned you wanted to do this with the exception of some enemies (bosses maybe?) so, the effect will not run if the target id(enemy database) is 3. I placed some numbers in red so you can change them as you want, in this case you need to change the enemy id for the enemies youw ant to avoid being hit for twice the damage. (If you want to add more enemy ids you need to add "&& target._enemyId != X" for every enemy you want to exclude, example "if (b.hp <= b.mhp * .20 && target._enemyId != X && target._enemyId != X && target._enemyId != X) im sure there is a cleaner way, but i dont know how.

For Skill2 -
Code:
<Pre-Damage Eval>
if (b.result().isHit()) {
if (b.hp <= b.mhp * .20 && target._enemyId != 3) {
target.gainHp(-99999);
 }
}
</Pre-Damage Eval>
* This one will instakill the target if the target has its hp on 20% or below its max. You can also exclude enemies here.

For Skill3-
Code:
<Pre-Damage Eval>
if (b.result().isHit()) {
if (b.hp <= b.mhp * .20 && b.isStateAffected(15) && target._enemyId != 3) {
target.gainHp(-99999);
 }
}
</Pre-Damage Eval>
* This one will instakill the target if the target has its hp on 20% or below its max AND has the state 15 on them(I think you were using state number15? if not, just change it. You can also exclude enemies here.

Now like I said, im using all of Yanfly plug ins, so I hope this work for you. If not, we will have to figure out which plug ins im using and you are not.

Edit: Oh, also. You noticed that for the instakill i dont add the state1 instead I do max damage, if somehow your project can go above 99999 dmg, change that number as well.
GL!
I dont know what this means :S
That code was only meant to help narrowing down the cause of your effects not working (you can check the contents of that game variable by using a "Message Text" command containing only "\v[10]" or by looking it up in the debug menu if MV still features that - in previous makers you could press F9 to bring up a screen containing all switches and variables).
So if the variable contained something unexpected afterwards (like "undefined" or if it was empty) the plugin probably would not have been set up correctly or your after-eval block was for some reason not recognized.

Of course, since you already figured out that the code works without another plugin, that command as well as the idea to put nonsense in your after-eval field are expendable for now. 🙂


As for the Battle Core plugin: All I can recommend as a first step would be to make sure those plugins are placed in the order they appear on Yanflys plugin list (e.g. the battle plugins should be above Skill Core). I haven't really found a possible cause for that specific issue so far.
If the problem persists, you could also try to ask for help in the "Javascript/Plugin Support" section.


When I brought up the Skill Core plugin I didn't know about your use of formula effects. You could just use another <formula effect> block for the execution effect of course (those are executed in order of their appearence in your note box, so you also have some control when to execute it).
You only really need the if (b.result().isHit()) { ... } block to check if the attack was a hit (if at all) in your after-eval effects. Pre-/Post-Damage- as well as the formula effects are only executed when the skill hit the target in the first place. Doesn't hurt to do so anyway of course. 🙂
Ok, I hope this is what you want. I have all of Yanfly Plug ins, and all of them are On (Including the Battle core, of course). So it should work for you.

For Skill1-
Code:
<Pre-Damage Eval>
if (b.result().isHit()) {
if (b.hp <= b.mhp * .20 && target._enemyId != 3) {
value = value * 2;
 }
}
</Pre-Damage Eval>
* This one will multiply the damage by 2 if the target has its hp on 20% or below its max. Also, you mentioned you wanted to do this with the exception of some enemies (bosses maybe?) so, the effect will not run if the target id(enemy database) is 3. I placed some numbers in red so you can change them as you want, in this case you need to change the enemy id for the enemies youw ant to avoid being hit for twice the damage. (If you want to add more enemy ids you need to add "&& target._enemyId != X" for every enemy you want to exclude, example "if (b.hp <= b.mhp * .20 && target._enemyId != X && target._enemyId != X && target._enemyId != X) im sure there is a cleaner way, but i dont know how.

For Skill2 -
Code:
<Pre-Damage Eval>
if (b.result().isHit()) {
if (b.hp <= b.mhp * .20 && target._enemyId != 3) {
target.gainHp(-99999);
 }
}
</Pre-Damage Eval>
* This one will instakill the target if the target has its hp on 20% or below its max. You can also exclude enemies here.

For Skill3-
Code:
<Pre-Damage Eval>
if (b.result().isHit()) {
if (b.hp <= b.mhp * .20 && b.isStateAffected(15) && target._enemyId != 3) {
target.gainHp(-99999);
 }
}
</Pre-Damage Eval>
* This one will instakill the target if the target has its hp on 20% or below its max AND has the state 15 on them(I think you were using state number15? if not, just change it. You can also exclude enemies here.

Now like I said, im using all of Yanfly plug ins, so I hope this work for you. If not, we will have to figure out which plug ins im using and you are not.

Edit: Oh, also. You noticed that for the instakill i dont add the state1 instead I do max damage, if somehow your project can go above 99999 dmg, change that number as well.
GL!

Woo Hot damn it works! Thanks buddy! I tried the 3rd skill which was the most conditions so im assuming the rest will work easily. Best it works will all my plugins 🙂 I must have been doing the code wrong. Its way to complicated for me some of yanfly needs it to say b and some need to say target.

Quick question, How come it shows the damage of the skill used I thought since it was pre-damage it would apply the code before the damage. It does not make all that much of a difference just wondering.
Also is it possible to play an animation to show the player that the enemy did not just die from skill damage and was "assassinated". Some visual cue would be nice but I could live without it.

That code was only meant to help narrowing down the cause of your effects not working (you can check the contents of that game variable by using a "Message Text" command containing only "\v[10]" or by looking it up in the debug menu if MV still features that - in previous makers you could press F9 to bring up a screen containing all switches and variables).
So if the variable contained something unexpected afterwards (like "undefined" or if it was empty) the plugin probably would not have been set up correctly or your after-eval block was for some reason not recognized.

Of course, since you already figured out that the code works without another plugin, that command as well as the idea to put nonsense in your after-eval field are expendable for now. 🙂


As for the Battle Core plugin: All I can recommend as a first step would be to make sure those plugins are placed in the order they appear on Yanflys plugin list (e.g. the battle plugins should be above Skill Core). I haven't really found a possible cause for that specific issue so far.
If the problem persists, you could also try to ask for help in the "Javascript/Plugin Support" section.


When I brought up the Skill Core plugin I didn't know about your use of formula effects. You could just use another <formula effect> block for the execution effect of course (those are executed in order of their appearence in your note box, so you also have some control when to execute it).
You only really need the if (b.result().isHit()) { ... } block to check if the attack was a hit (if at all) in your after-eval effects. Pre-/Post-Damage- as well as the formula effects are only executed when the skill hit the target in the first place. Doesn't hurt to do so anyway of course. 🙂
Thanks for all your help its up and running 🙂
Ha, to be honest i wanted the damage not to show, because well, its way cooler to just kill the target, but I couldnt achieve it. Well have to find a way, Im sure it can be done. For the animation, yeah, it is: target.startAnimation(N); where N is the id of the animation, place it where you need it.

Also, I recommend you this:

https://www.youtube.com/channel/UCxqti0F9VuiSWyqznaua_zA/playlists

Playlist: Tricks And Tips

Those are some pretty custom skills made by yanfly, the cool part is that you can see the custom code used and you can tweak it and learn how to create yours.

You are welcome, happy it worked.
Quick question, How come it shows the damage of the skill used I thought since it was pre-damage it would apply the code before the damage.
The code is applied before the damage, but that doesn't stop the damage from being applied afterwards anyway.
When gaining/losing HP, the total amount (including any excess) is stored in the targets result sheet, which is displayed after the hit was processed.
When a target gains/loses HP multiple times in a single hit, the latest hit takes precedence over earlier gains in the result (hence the 99999 damage are overridden when the actual damage is applied).

I haven't tried it, but if you don't want to show any damage, you could try to wipe the HP damage from the targets result after the damage was applied:
<Post-Damage Eval>
b.result().hpAffected = false;
b.result().hpDamage = 0;
</Post-Damage Eval>


(The first line of code should already be enough here though)

Its way to complicated for me some of yanfly needs it to say b and some need to say target
I don't know about the rest of Yanflys plugins, but at least as far as the Skill Core plugin goes it's up to you whether you use b or target in your code. They both mean the same.
The code is applied before the damage, but that doesn't stop the damage from being applied afterwards anyway.
When gaining/losing HP, the total amount (including any excess) is stored in the targets result sheet, which is displayed after the hit was processed.
When a target gains/loses HP multiple times in a single hit, the latest hit takes precedence over earlier gains in the result (hence the 99999 damage are overridden when the actual damage is applied).

I haven't tried it, but if you don't want to show any damage, you could try to wipe the HP damage from the targets result after the damage was applied:
<Post-Damage Eval>
b.result().hpAffected = false;
b.result().hpDamage = 0;
</Post-Damage Eval>


(The first line of code should already be enough here though)

I don't know about the rest of Yanflys plugins, but at least as far as the Skill Core plugin goes it's up to you whether you use b or target in your code. They both mean the same.

WOW awesome thanks it looks amazing. How do you know all these codes? I need to learn that because my understanding is awful lol, is there a good place to improve my skills?

I wrote it like this:

<Post-Damage Eval>
if (b.result().isHit()) {
if (b.hp <= b.mhp * .90 && b.isStateAffected(16) && target._enemyId != 3) {
target.startAnimation(65); target.gainHp(-99999); b.result().hpAffected = false; b.result().hpDamage = 0;
}
}
</Post-Damage Eval>
So it all works out perfect now 🙂
GREAT! Now I can get rid of that pesky 0 showing on some skills that I NEEDED to be hp damage but with no damage showing.

Original thread: https://forums.rpgmakerweb.com/threads/execute-skill-kills-target-if-hp-is-lower-then-x.96498/