# Calculation of damage with debuffs

#### Kes

##### Veteran
My understanding is that debuffs, like states, are applied after damage has been calculated. However, I'm now wondering if that is actually true.

I have a skill where the actor is healed by the enemy's ATK x 2. It also applies an ATK debuff. For the healing to be worth it, that debuff must happen after the actor gets the heal. However, it doesn't seem to be quite like that.

I have an enemy with an ATK of 30. Healing should, if I am correct, be 60. It never is. With variance set to zero, it is 46, which looks like it's being calculated after the debuff has been applied.

Here is my damage formula:
Code:
``a.hp += b.atk*2; b.add_debuff(2,2,); b.atk*2``

As you can see, the damage part of the formula is at the end, after the debuff, but that is because the formula has to end with a number. If I end it with the debuff, I get zero damage.

Can anyone enlighten me as to what's going on here?
Thank you.

#### kirbwarrior

##### Veteran
My understanding is that debuffs, like states, are applied after damage has been calculated.
I didn't know that. I know common events happen after damage, but I thought buffs and states applied right when they were received.

Code:
``a.hp += b.atk*2; b.add_debuff(2,2,); b.atk*2``
And as such, you're applying the debuff before the damage but after the healing. You could set a temporary variable first;

Code:
``c = b.atk*2; a.hp += c; b.add_debuff(2,2,); c``

"c" is set up to be the target's attack. In order, it then heals the user for c, applies a debuff, then damages for c.

Mind, that last part makes me pause. It sounds like what you want is to only heal and debuff, not do damage. If that's true, I'd actually make this two different skills;

A - This skill targets the enemy. It gives an invisible state (for simplicity, you can call this state Target and use it for other skills).. It then calls a common event that stores the target's attack into a variable (using Target to 'find' the right enemy), then forces skill B.
B - The user heals equal to the variable, then gives the Target'd enemy a debuff and removes Target. This might need a second common event.

That's without plugins, I'm certain it's much easier with plugins.

#### caethyril

##### ^_^
Since the debuff is being added to the target and seems to have fixed duration, I wonder if it might be easier to just add it as a skill effect?

#### Magnus0808

##### Software Developer
Hmm I just tried to replicate the issue, but when using a skill with your damage formula against an enemy with 30 ATK I did heal for 60 as expected.

Kes

#### Kes

##### Veteran
@Magnus0808 That confirms my logic, so thank you for the test. I'm not at all sure what is causing the difference at my end.

@kirbwarrior I tried your damage formula and that appears to have sorted it out, so thank you very much.

@caethyril I'll keep that in mind for other skills where I want to do something like this. Thank you.

#### kirbwarrior

##### Veteran
Since the debuff is being added to the target and seems to have fixed duration, I wonder if it might be easier to just add it as a skill effect?
This actually sounds way easier. Since the attack heals the user and damages the target, it could just be a drain spell that does "b.atk * 2" damage, then adds the debuff with effects instead of the damage formula, which should come after the damage.

Kes

#### Kes

##### Veteran
@kirbwarrior That does indeed sound much more straightforward. I was simply overthinking it. That's what I get for being away from game making for a few months.
Thank you.

#### Magnus0808

##### Software Developer
Maybe I misunderstod what the skill was supposed to do. I thought it was on purpose that you deal less damage than you heal.
So what your damage formular does is (when the target has 30 ATK):
Heal for 2 * target ATK (60)
Apply ATK debuf to target for 2 rounds (reduces ATK to around 22)
Deal 2 * target ATK (44)

The reason this happens is because the damage formular gets evaluated (basically run as code) where the last statement is returned as the damage value. So the first two statements (Lines before ; ) gets run first in order.

#### gstv87

##### Veteran
in terms of objects, "b" is always the same object.
if you apply a modifier to "b", and then reference "b", you're referencing "b" + modifier, not "b, the object that is stated in the database"

if there's no separation of references, the program doesn't know that you want to keep them separated.

### Latest Profile Posts

RPG Maker News #11 | Mech Battlers, Angels of Death on Console, New MZ Plugins & Music Packs

forgot I drew Riley's message bust ages ago. They might be one of the most unique characters in the game so far.
37 years old...
What hath possessed me to start rapping, Lloyd only knows.

I think the worst part is, I am actually really good...

no... actually, the worst part is writing a song about bum rushing Gandalf to stop Frodo...
I don't even listen to rap.
Just finished v0.1 of FOSSIL. It's a plugin that lets you use MV plugins in MZ without editing them by making them use new functions.

I have about 40 plugins tested and working so far (including chrono engine and some other big ones). I hope that the community will pitch in and help improve it, so we can use all the old plugins in MZ.
More suggestions for RMMZ in my rambling/complaints topic! Come to see it! https://forums.rpgmakerweb.com/inde...-rm-veteran-suggestions-and-solutions.135408/