Setting and retrieving a local variable within damage formula

Hello! I have a skill that needs to deal heavy HP damage based on the current hp of the user before the user self destructs.

For reference, this skill works like the combination of pokemon moves Explosion and Eruption.

The problem is, I must set HP to zero and perform collapse before dealing damage. But if I do that, the HP value will be zero and do no bonus damage to enemies.

I will need to set a variable that will store the current hp before collapse. I was told that damage formula uses semi scripting language, so I figured I first would initialize the variable, set the variable, then retrieve it during the damage calculation.

Here's my code:

JavaScript:
let var = a.hp; a.setHp(0); a.performCollapse(); (var) + (a.mat * 2) - b.mdf * 2

or


JavaScript:
var = a.hp; a.setHp(0); a.performCollapse(); (var) + (a.mat * 2) - b.mdf * 2

Either way, this does zero damage and does not collapse the user.


JavaScript:
a.setHp(0); a.performCollapse(); a.hp + (a.mat * 2) - b.mdf * 2

This functions sort of correctly, but deals almost no damage.

What gives? How do I make this work properly?
You cannot use keywords as variables (let and var are keywords reserved by JavaScript to declare variables - you need a variable name to follow those declarations - I will use var as an example below).

The last formula you used has 0 as the hp for the user in the calculation immeditely following the collapse function which means the damage is 2 times the user's magic attack minus 2 times the target's magic defense.

Try this:
JavaScript:
var dmg = a.hp; a.setHp(0); a.performCollapse(); dmg + (a.mat * 2) - b.def * 2
OOPS! Yeah, I figured it was something as simple as that!

By the way, what's the difference between let
JavaScript:
let x = a.hp
and
JavaScript:
var x = a.hp
?
For this specific purpose in the damage formula, you won't notice a difference between the two.

Pretty much I use 'const' when I know the variable won't change and 'let' when the variable can/will/might change. I usually only use 'var' to keep up with code compatibility with plugins (sometimes simple script calls too because it gets evaluated and then it is done) or when I am using it in a global scope like declaring a plugin name space.

If you are interested, you can read more here:
By the way, what's the difference between
actually you don't need either because you don't need to store the value after the damage formula.
this will also work:
JavaScript:
dmg = a.hp; a.setHp(0); a.performCollapse(); dmg + (a.mat * 2) - b.def * 2
actually you don't need either because you don't need to store the value after the damage formula.
this will also work:
JavaScript:
dmg = a.hp; a.setHp(0); a.performCollapse(); dmg + (a.mat * 2) - b.def * 2
In JavaScript, assigning to a non-existent variable causes that variable to be implicitly declared on the global scope. That allows it to persist for the rest of the play session, rather than being deleted with the source context. Which, like you say, is unnecessary (at best!).

Like bass2yang said, it is much better practice to use one of the local declarators: const, let, or var. These declare the variable within the eval's sandbox, so it gets deleted after the formula is done processing~
Thanks so much! This answered my question and I was able to produce the desired effect properly.

Original thread: https://forums.rpgmakerweb.com/threads/setting-and-retrieving-a-local-variable-within-damage-formula.177336/