Storing damage dealt to target for the duration of a state in a variable

Hi everyone,

I'm looking to make a state that lasts for a certain amount of time, and then when it's complete, I wish to store the amount of HP damage it received during that time into a $gameVariable. The state cannot be removed by any restriction or other circumstances.

The only part I am struggling with is how to store the received damage during the time the state was active into a variable when it finishes. I believe YEP's Buffs & States core can achieve this with a custom effect and a bit of scripting, but that's where I hit a wall, as I'm not proficient with scripting.

Would anyone be able to make any suggestions?

Thank you!
Do you want it to include healing? I.e. if you got the state at 50 HP, took 20 damage, then healed +10 HP, would your variable be 10 or 20?

If you just want the overall difference in HP between application and removal, I think that's fairly easy, try something like this~ 😤
Code:
<Custom Apply Effect>
user._originalHp = user.hp;
</Custom Apply Effect>

<Custom Remove Effect>
$gameVariables.setValue(1, user._originalHp - user.hp);
user._originalHp = undefined;
</Custom Remove Effect>

If you want something to track every damage instance, this Tips & Tricks article may be a good start, just put the result into a variable (as above) instead of reapplying the healing effect. Not sure it tracks any damage from non-active sources though, e.g. scripted damage or negative HP regen: http://www.yanfly.moe/wiki/False_Promise_(MV_Plugin_Tips_&_Tricks) 😅
@caethyril again to the rescue! Thank you so much.

Actually, I only want to include the damage received, and not any HP recovered over that time (through any means). So to answer your question, it should be 20.

So I'm looking into the second solution at the moment, but I'm just a bit confused as to what I do with the excess if statements. If I were to have a crack at it, it would look like:

// To prevent infinitely reapplying the effect and making a target invulnerable to direct damage.
<Reapply Ignore Turns>

<Custom Apply Effect>
// Sets the default promised damage to 0 if it is undefined.
target._promisedDmg = target._promisedDmg || 0;
</Custom Apply Effect>

<Custom React Effect>
// If the receiving action is an HP damage effect...
if (this.isHpEffect()) {
// Sets the default promised damage to 0 if it is undefined.
target._promisedDmg = target._promisedDmg || 0;
// If the damage is positive...
if (value > 0) {
// Then, add it to the promised damage value.
target._promisedDmg += value;
} else {
}
value = 0;
}
</Custom React Effect>

<Custom Remove Effect>
// Sets the default promised damage to 0 if it is undefined.
target._promisedDmg = target._promisedDmg || 0;
// If the promised damage is positive...
if (target._promisedDmg > 0) {
// Then play a damage animation.
target.startAnimation(11);
// If it isn't positive...
} else {
}
// Alter the target's HP based on the damage received.
target.gainHp(-target._promisedDmg);
// Start the damage popup.
target.startDamagePopup();
// Clear the target's result.
target.clearResult();
// If the target is dead...
if (target.isDead()) {
// Play the collapse animation on the target.
target.performCollapse();
}
target._promisedDmg = undefined;
</Custom Remove Effect>

I think that would do it, or do I need to put something in where the else statements would have been?

Also, am I able to rename target._promisedDmg?

In the following line:

target._promisedDmg = target._promisedDmg || 0;

just creating the string "target._promisedDmg" ?

Thanks!
Oh yes, I don't think I really looked at the article, most of that example's actually irrelevant. Sorry for the confusion! 😴

Try something like this (edit the variable ID in the Remove Effect as needed)~
Code:
<Custom Apply Effect>
target._dmgCount = target._dmgCount || 0;
</Custom Apply Effect>

<Custom React Effect>
// Only count HP damage
if (this.isHpEffect() && value > 0) {
  target._dmgCount += value;
}
</Custom React Effect>

<Custom Remove Effect>
// Store value in game variable 5
$gameVariables.setValue(5, target._dmgCount);
target._dmgCount = undefined;
</Custom Remove Effect>
As mentioned, this'll only count damage from active sources, i.e. skills that target the user. DoT etc will not trigger the React Effect, thus will be ignored. I don't think there's an easy way around that...if needed then maybe you could add a new step to the damage formula using Yanfly's Damage Core plugin to handle it? Not sure. 😅

Also, am I able to rename target._promisedDmg?

In the following line:
Code:
target._promisedDmg = target._promisedDmg || 0;
just creating the string "target._promisedDmg" ?
Yep! Well, it's a number, not a string, but JS isn't strictly-typed; it works much the same with numbers or strings. 🙂

That line of code says "set the target's _promisedDmg value to its current value, or 0 if the current value is falsy". Basically, if _promisedDmg doesn't exist yet on the target (i.e. it's "undefined"), it'll be set to 0. 😮‍💨
Hi again @caethyril !

I've been doing some tests with the proposed solution, and I made a slight re-adjustment to the custom effects - specifically, at the very end. I changed the Custom Remove Effect to a Custom Leave Effect, and added a few lines to force an action, see here:

<Custom Apply Effect>

target._dmgCount = target._dmgCount || 0;

</Custom Apply Effect>

<Custom React Effect>

// Only count HP damage

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

target._dmgCount += value;

}

</Custom React Effect>

<Custom Leave Effect>

// Store value in game variable

$gameVariables.setValue(81, target._dmgCount);

target._dmgCount = undefined;

var skill = 72;

var target = -1;

BattleManager.queueForceAction(user, skill, target);

</Custom Leave Effect>

Now this pretty much works right up to the point of actually dealing damage with the skill I've created (skill 72). All this skill does is target 1 enemy and halve the amount of damage in the variable, with this formula:

[v]81 / 2

But upon delivering the hit, it does exactly 0 damage. Do you have any thoughts on why this is?

EDIT: And just to let you know, this was damage from stock standard "attack" skills, no DoT stuff.

EDIT EDIT: I even tried using my noodle a bit more, and thought "well maybe it's trying to process everything at the same time", so I set up a common event that ran the last three lines separately, and then called that common event in the Custom Leave Effect, but I still get the same result. Can't work out where I'm going wrong.
All this skill does is target 1 enemy and halve the amount of damage in the variable, with this formula:
[v]81 / 2
But upon delivering the hit, it does exactly 0 damage. Do you have any thoughts on why this is?

The correct syntax to reference a variable in a damage formula is:
Code:
v[81] / 2
What you had currently is a syntax error, and any error will cause the damage formula to return zero.
Oh, what an obvious mistake that I was overlooking. Thanks so much for pointing that out, @Aloe Guvner !
Thread closed IgnoreMe

Original thread: https://forums.rpgmakerweb.com/threads/storing-damage-dealt-to-target-for-the-duration-of-a-state-in-a-variable.113115/