MV State that heals upon recieving damage

Heyo! 🙂 I'm facing a little issue which I feel should be simple, but has had me stuck for the past few hours...

Using Yanfly's Buffs & States Core and their Thornmail trick I thought about making a Guard command that heals x% the user's HP when getting hit. Everything works until the healing part occurs. Here's what's written in my State note:

<Custom React Effect>
if (value > 0) {
var heal = target.mhp * 0.1;
var heal = Math.ceil(heal);
target.gainHp(heal);
}
</Custom React Effect>

If I write user.gainHp(heal), the ennemy is indeed healed 10% of the character's hp, so the problem doesn't seem to come from above. But for some reason when I put target (or defender, or actor, or even $gameActors.actor(2) ) it doesn't work. I tried using <Custom Respond Effect> but to no avail.

As someone who doesn't know javascript, I was quite proud of "successfully" tweaking the Thornmail code from guessing what each line does, but now I'm stuck and I don't even understand why... 😢
Try this:
JavaScript:
<Custom React Effect>
if (value > 0) {
var heal = Math.ceil(target.mhp * 0.1);
target.gainHp(heal);
}
</Custom React Effect>
I think the issue was that you had "var heal" twice so the 1st instance got ignored, or rather replaced by the subsequent "var heal".
I took liberty and I made some additional fixes to your code @Frostorm 🙂 Of course, it will work, but I'm bad at explaining things, so I belive you understand my fix 🙂

Code:
<Custom React Effect>
if (this.isHpEffect()){
if (value > 0) {
var heal = Math.ceil(target.mhp * 0.1);
target.gainHp(heal);
}
}
</Custom React Effect>

All I can say, without that condition isHpEffect() if the spell would damage MP instead of HP, target still would recieve some healing values.
All I can say, without that condition isHpEffect() if the spell would damage MP instead of HP, target still would recieve some healing values.
Yes, I personally always include "isHpEffect" as well, but I didn't in this case since OP didn't. Here's a more streamlined version:
JavaScript:
<Custom React Effect>
if (value > 0 && this.isHpEffect()) {
var heal = Math.ceil(target.mhp * 0.1);
target.gainHp(heal);
}
</Custom React Effect>
Also also, this still isn't perfect though. When state is in effect and you use for example healing item - now I'm not 100% sure, because I don't have single state that uses <custom react effect> - but someone (enemy or caster) will recieve additional healing value upon using healing potion (item) 🙂
Also also, this still isn't perfect though. When state is in effect and you use for example healing item - now I'm not 100% sure, because I don't have single state that uses <custom react effect> - but someone (enemy or caster) will recieve additional healing value upon using healing potion (item) 🙂
Hmm, isn't that why we included "value > 0"? So that only stuff that decreases HP would trigger the effect. A healing pot would have a value < 0 (i.e. a negative value). So it should be fine, however, if an item did have a negative value (drinking a poison potion maybe? lol) then that would probably trigger the effect. Maybe this might work though:
JavaScript:
<Custom React Effect>
if (value > 0 && this.isHpEffect() && (this.isPhysical() || this.isMagical())) {
var heal = Math.ceil(target.mhp * 0.1);
target.gainHp(heal);
}
</Custom React Effect>
Wow thank you two for the quick replies!

I didn't think healing would be issue but just in case I'll leave it there ! I didn't even think about MP damage, something I would have realised much much later, if at all haha

I tried the last code but it still won't target my character somehow. Though everything before seems to work since I also tried with "user" and it did heal the mob... I guess shortening the var heal doesn't hurt anyway 😀
@Frostorm Variable "value" stores actual damage dealed to battler and isHpEffect() checks if hp was damaged/healed because game engine treats at the same category (level) healing/damaging items and the skills. For example, this is my state, wich gives an actor exp from half the damage he deals to enemy:
Code:
<Custom Establish Effect>

if (this.isHpEffect() && value > 0){

user.gainExp(Math.floor(value * 0.5));

$Soulrender.sayPop("\\i[159] Andvari's ring gave to " + user.name() + "  " + Math.floor(value * 0.5) + "XP");

}

</Custom Establish Effect>

when i used healing potion my actor lost 100XP (potion heals 200HP), so that's why is so important to check some certain factors before applying effect.
So I used a different method, it works! But it's the clunkiest thing I've ever seen. From a player point of view it's actually quite good, but god the system is terrible :LZSlol:

JavaScript:
<Custom React Effect>
if (value > 0 && this.isHpEffect() && (this.isPhysical() || this.isMagical())) {
$gameTemp.reserveCommonEvent(27);
}
</Custom React Effect>

This Common Event forces an action. I use that bodgy method quite a lot so I have a dedicated invisble character (turns out the action works even if the actor isn't in the party).
So they use an action, in the note tag of which there is this bit (from Yanfly's Action Sequence):

<target action>
animation 201: character 2
HP +10%: character 2, show
</target action>

Now if it were just me I'd call it a day :LZSangel: It works, there's no delay and I could even add a little animation, but I understand it's quite the mess. Being able to force the action immediatly from the Custom Reaction would a be a plus (but I don't understand the force action script call).

Luckily in my case, there's only one character able to do this, but if someone were to try something similar in a job based game, that wouldn't work, unless they make a different state for each character... Quite tedious. And it wouldn't even work for mobs.

I'm still annoyed the target.gainHp(heal) didn't work. If "target" was propely selected when defining the varariable mhp * 0.1, why didn't it work when actually healing? Javascript will forever remain a mystery to me :LZSoops:

Original thread: https://forums.rpgmakerweb.com/threads/state-that-heals-upon-recieving-damage.129053/