How to drain Stamina(MP) based off of mitigated damage

Hey so I what I've been trying to accomplish is when anyone is physically attacked in combat the damage is initially mitigated by defense, and then applied to health, but I wanted the amount of mitigated damage to still be applied to their MP gauge(which I've repurposed as stamina and is used as basically a second more short term HP gauge). So I've came up with the following to use as the damage formula:

var wd = Math.randomInt(a.atk)+1; //Weapon Damage
var dm = Math.randomInt(b.def)+1; //Damage Mitigation
dm > wd ? b.gainMp(-wd): b.gainMp(-dm); //lose stamina based off of mitigated damage or weapon damage if value is lower
b.result().critical ? wd : Math.max(1, (wd - dm)) //criticals bypass defense

And it works, but one of the problems with the above is using b.gainMp in the damage formula step doesn't account for resistances to damage types, which I'd still like to be considered, or anything else that is applied after the formula I suppose. I'm learning this as I go, so it might be super simple, but I've tried using my var dm later in the action phase (with VisuStella Battle Core, <JS Post-Damage as User> , but that doesn't seem to work the way I've tried it. which was something like:

<JS Post-Damage>
dm > wd ? b.gainMp(-wd * this.calcElementRate(target)): b.gainMp(-dm * this.calcElementRate(target))
</JS Post-Damage>

But the console says dm is undefined at that point. So really what I want is the amount of damage that was mitigated by defense to be applied to their MP gauge but also affected by the modifiers that come after the damage formula is calculated. Maybe this can all be done a completely different way than I've been trying.

I'd really appreciate the help, thank you.
But the console says dm is undefined at that point. So really what I want is the amount of damage that was mitigated by defense to be applied to their MP gauge but also affected by the modifiers that come after the damage formula is calculated. Maybe this can all be done a completely different way than I've been trying.
generally speaking, any variable you define wont exist anywhere outside of where you made it. Im assuming that the damage formula is some kind of plugin parameter? im not very familiar with the VS suite. it looks like its ran off of an action, so what you could probably do is write those variables as properties of this instead and try to use them inside of the post damage notetag.
Code:
var wd = Math.randomInt(a.atk)+1; //Weapon Damage
var dm = Math.randomInt(b.def)+1; //Damage Mitigation
dm > wd ? b.gainMp(-wd): b.gainMp(-dm); //lose stamina based off of mitigated damage or weapon damage if value is lower
b.result().critical ? wd : Math.max(1, (wd - dm)) //criticals bypass defense

this._wd = wd
this._dm = dm

Code:
<JS Post-Damage>
this._dm > this._wd ? b.gainMp(-this._wd * this.calcElementRate(target)): b.gainMp(-this._dm * this.calcElementRate(target))
</JS Post-Damage>
Im assuming that the damage formula is some kind of plugin parameter?
I've just been putting it in the skill damage formula box. Each weapon has it's own individual weapon skill to be used as its default attack, rather than using a universal attack command skill.
After trying what you suggested I put this in the formula box in the database:

var wd = Math.max(1, Math.randomInt(a.atk)+1); var dm = Math.max(0, Math.randomInt(b.def)); this._wd = wd; this._dm = dm; b.result().critical ? wd : Math.max(1, (wd - dm))

and put this in the skill note tag box:

<JS Post-Damage>
this._dm > this._wd ? b.gainMp(-this._wd * this.calcElementRate(target)): b.gainMp(-this._dm * this.calcElementRate(target))
</JS Post-Damage>

And as far as I can tell it works! I really appreciate the help, thank you!

Original thread: https://forums.rpgmakerweb.com/threads/how-to-drain-stamina-mp-based-off-of-mitigated-damage.179539/