MV Need Help with Damage Returned as Healing

Hi, I'm making an RPG that centralizes a core mechanic where all forms of damage is returned to the sender in the form of healing. I want to know how I can get it done for other variants like actors gaining less or no health at all, enemies being able to also utilize this mechanic to heal off of damage, and enemy variants minimizing the healing benefit when it comes to attacks.

I've read numerous threads that focused on coding HP Draining skills but I wanted the primary focus to be that *all* forms of damage return healing, even basic attacks and magic attacks but I couldn't quite get it done correctly since I'm a beginner when it comes to programming and also RPG Maker MV. I know this mechanic seems weird but I just wanted to figure this problem out and see if there was any immediate difference to it over the other threads I've read online about it that are several years old on HP Draining skills.

Any plugin recommendations will also do.
I do use this mechanic for some of my abilites aswell.
For that I mostly use the damage formulas themselves.
Here is a template that I often use:

a.gainHp(Math.round((X)*Y)); Math.round(X)

X = damage Formula ( something like a.atk * 2 - b.def)
Y = healing multiplier ( something like 0.5 to heal for half of damage dealt)
I often use a.rec for Y to get the recovery rate and sometimes I combine both.

The end result can look something like this:
a.gainHp(Math.round((a.atk * 2 - b.def)*a.rec*0.5)); Math.round(a.atk * 2 - b.def)

Some more explainations:
#1 The first part of the formula (a.gainHp) is the function that lets you heal (or take damage if you input negative numbers.)
#2 Math.ceil rounds the number (you need to do this to avoid janky numbers)
-> You may also use Math.ceil or Math.floor instead (If you always want to round up or down)
#3 The part after the " ; " is the actual damage formula you usually use to make skills

Edit: I don't think you actually need to round the numbers in the second part since the engine does that automatically. So you can just leave it like that:

a.gainHp(Math.round((X)*Y)); X
I do use this mechanic for some of my abilites aswell.
For that I mostly use the damage formulas themselves.
Here is a template that I often use:

a.gainHp(Math.round((X)*Y)); Math.round(X)

X = damage Formula ( something like a.atk * 2 - b.def)
Y = healing multiplier ( something like 0.5 to heal for half of damage dealt)
I often use a.rec for Y to get the recovery rate and sometimes I combine both.

The end result can look something like this:
a.gainHp(Math.round((a.atk * 2 - b.def)*a.rec*0.5)); Math.round(a.atk * 2 - b.def)

Some more explainations:
#1 The first part of the formula (a.gainHp) is the function that lets you heal (or take damage if you input negative numbers.)
#2 Math.ceil rounds the number (you need to do this to avoid janky numbers)
-> You may also use Math.ceil or Math.floor instead (If you always want to round up or down)
#3 The part after the " ; " is the actual damage formula you usually use to make skills
Oh, this is precisely what I was looking for! Thank you so much! I'll see if I can figure out the rest of it from over here, but this is pretty much what I needed!
Great I could help 🙂
If you have any more questions, feel free to ask and I'll help you as best I can.
Though I am no expert by any means.

One thing I need to mention however;

This way of healing off damage does not take into account any variance you put into the skill.
You know, if you have a skill that deals 100 damage with 20% variance,
the skill will deal 80 - 120 damage but you will ALWAYS heal for 100.
Great I could help 🙂
If you have any more questions, feel free to ask and I'll help you as best I can.
Though I am no expert by any means.

One thing I need to mention however;

This way of healing off damage does not take into account any variance you put into the skill.
You know, if you have a skill that deals 100 damage with 20% variance,
the skill will deal 80 - 120 damage but you will ALWAYS heal for 100.

Understood! I do have a question or two regarding the healing. Is it technically possible referencing the original code to create an adjustable immunity for certain actors and enemies alike? For example, Enemy A and Actor B have similar immunities where they take damage but the attacker doesn't get healing back, or certain actors get less/more healing than the others?

Again, thank you so much for taking time to help me out with this issue, it means a lot to me!

Edit: Forgot my other question...
Hmm... I am not sure. This is a more complex matter that I am not sure can be handled elegantly with
just the damage formula.

There are two ways to do this in my mind:
Also, things will get pretty messy here:

#1 Check if a character is afflicted with a state and use different formulas:

if (a.isStateAffected(Z)) {a.gainHp(Math.round((X)*Y)); X} else { a.gainHp(Math.round((X)*Y2)); X}

This formula checks if the user of the skill is afflicted with state ID Z. If he is, we use the first formula.
If he is not, we use the second formula.
For instance, Y would be something like 0.5 and Y2 would be something like 1.

So you could use an invisible "placeholder" state Z (ID of the state) and apply it to anyone you like
at the start of combat.

#2 Use a parameter you don't need to apply custom healing rates:

There are some special parameters you might not use, these might include:

Target Rate - tgr (adjusts how often enemies target that character)
TP Charge Rate - tcr (adjusts how quickly you gain TP)
Floor Damage - fdr (adjusts the damage you take from floor damage)

Of course this works with any parameter you don't have use for (special parameters work best though)

You can repurpose them as healing multipliers. Like so for example:

a.gainHp(Math.round((a.atk * 2 - b.def)*a.fdr)); a.atk * 2 - b.def

As you can see, we added a a.fdr multiplier to our healing. Now you can just go to every actor and enemy and set their Floor Damage parameter to any value you wish.
For example, giving an enemy a 50% Floor damage trait means he heals 50% less from all skills that have such a multiplier.

But I am sure there are more elegant ways to solve this. Maybe someone else can provide you better solutions.
Hmm... I am not sure. This is a more complex matter that I am not sure can be handled elegantly with
just the damage formula.

There are two ways to do this in my mind:
Also, things will get pretty messy here:

#1 Check if a character is afflicted with a state and use different formulas:

if (a.isStateAffected(Z)) {a.gainHp(Math.round((X)*Y)); X} else { a.gainHp(Math.round((X)*Y2)); X}

This formula checks if the user of the skill is afflicted with state ID Z. If he is, we use the first formula.
If he is not, we use the second formula.
For instance, Y would be something like 0.5 and Y2 would be something like 1.

So you could use an invisible "placeholder" state Z (ID of the state) and apply it to anyone you like
at the start of combat.

#2 Use a parameter you don't need to apply custom healing rates:

There are some special parameters you might not use, these might include:

Target Rate - tgr (adjusts how often enemies target that character)
TP Charge Rate - tcr (adjusts how quickly you gain TP)
Floor Damage - fdr (adjusts the damage you take from floor damage)

Of course this works with any parameter you don't have use for (special parameters work best though)

You can repurpose them as healing multipliers. Like so for example:

a.gainHp(Math.round((a.atk * 2 - b.def)*a.fdr)); a.atk * 2 - b.def

As you can see, we added a a.fdr multiplier to our healing. Now you can just go to every actor and enemy and set their Floor Damage parameter to any value you wish.
For example, giving an enemy a 50% Floor damage trait means he heals 50% less from all skills that have such a multiplier.

But I am sure there are more elegant ways to solve this. Maybe someone else can provide you better solutions.

I apologize for replying late, but thank you so much for helping me regardless. Maybe there *is* a more elegant way to do this, but for now I think this will do. I appreciate the help you've given me with this endeavor!
One thing that I just recently discovered, which should have been obvious to me.
When you have a formula like :

a.gainHp(Math.round((a.atk * 2 - b.def)*0.5)); a.atk * 2 - b.def

it is possible to damage yourself instead of healing.

Lets say you have 10 atk and the enemy has 50 def.
When we put these numbers into the formula, the outputs would be:

a.gainHp(Math.round(-15)); -30

And since the function gainHp() also accepts negative numbers, you would actually take 15 damage from using that skill on the enemy.

To solve this, I suggest you use this template instead (except if you don't care about this problem):

a.gainHp(Math.round(Math.max(0, X)*Y)); X

The added Math.max() function prevents negative numbers because it will pick the highest number you give to it (seperated by commas). By adding a 0 to the function along with your usual damage formula "X", you are guaranteed to never go below 0.


With the previous numbers, it would look like this:

a.gainHp(Math.round(Math.max(0, -15)); -30

When you use the skill now, it will still deal 0 damage, but atleast you will not damage yourself, because 0 is greater than -15.

Original thread: https://forums.rpgmakerweb.com/threads/need-help-with-damage-returned-as-healing.129537/