Calculation of damage with debuffs

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.
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.
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?
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. 😅
@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.
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.
@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.
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.
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.

Original thread: https://forums.rpgmakerweb.com/threads/calculation-of-damage-with-debuffs.133956/