Passive State trouble (+mhp per missing hp)

I've been beating my head against a wall with this script for about a week now and can't figure it out.

I'm trying to set up a passive state (using Yanfly's engine, equippable states, skill core, parameter core, and many others) that boosts Max Hp based on the hp you've currently lost (its 1 for 1 so if you lose 300 hp, you gain 300 max hp while you're down 300 hp).

however, it keeps repeating the "less than or equal to" loop (that was the reason I put 2 different animations in it, for debugging purposes).

<Custom Regenerate Effect>

var maxHp = maxHp || 0;

if (maxHp <= 0){

var maxHp = target.mhp;

var damageTaken = target.mhp - target.hp;

user.startAnimation(12);

}

if (maxHp > 0){

var damageTaken = maxHp - target.hp;

user.startAnimation(11);

}

user.addStateCounter(stateId, damageTaken);

user.clampStateCounter(stateId, 0, 1000);

user.setMaxHpPlus(damageTaken);

</Custom Regenerate Effect>



<Custom Remove Effect>

var maxHp = 0;

</Custom Remove Effect>

I'm trying to set up a variable that only ever checks max hp once that then uses that variable as a substitute for max hp so that the max hp increase is consistent (though I still want the player to benefit from the hp boost on the first turn).

I'd like to get it to work as soon as damage has been dealt but I can't find a tag that will update it like that (only regenerate and deselect tags seem to update it properly)

Original thread: https://forums.rpgmakerweb.com/threads/passive-state-trouble-mhp-per-missing-hp.78592/