A Guide to Damage Formulas

Additional tutorial credits: @TheoAllen, for converting MV/Z formulas to Ace. The parallels are now included in the intro post.

If you're uncomfortable going much further than a.atk * 4 - b.def * 2 for your damage formulas, this is the guide for you. Damage formulas are extremely powerful tools that can let you create complex abilities. They are fairly similar to actual scripts in syntax (which is why they can be intimidating).

I placed this tutorial in Non-Maker Specific because it has worked with MV and VX Ace.

Basic Setup
[MV]
1645550910442.png

[VX Ace]
1645484383073.png
  1. Type. There are six different damage types: HP damage, MP damage, HP recover, MP recover, HP drain, and MP drain. They each should be fairly self-explanatory. Drain restores health to the user equal to damage dealt (not half as in some games).
  2. Element. Element allows you to select the damage element. Normal Attack is best for most physical moves (e.g., a skill like Wild Blow). Elements are important to set for most games because they can let you add in elemental weakness and effectiveness.
  3. Variance. Variance is a percentage that lets you set how much the damage will vary from what the formula says. If the formula computes to 50, and if variance is 20%, it can do 80-120% of 50 damage, so 40-60 damage. I recommend setting it to 10%.
  4. Critical. Critical allows you to select if the attack can have a critical. Most spells do not have this enabled, in most games. But do what you like!
Basic Formulas and Parameters​
Onto the meat of the tutorial!

Before we begin, note the importance of capitalization. Everything must be capitalized as directed in this thread. Do not randomly capitalize or un-capitalize as you desire. If you do, the formulas will not work.

Let's break down the standard attack formula:
Code:
a.atk * 4 - b.def * 2
  • "a" symbolizes the user, and "b" symbolizes the targets.
  • Think of the period as an apostrophe + s: 's.
  • In other words, "The damage dealt is a's attack multiplied by 4, minus b's defense multiplied by 2." Pretty simple.
    • Say the user's attack is 20, and the enemy's defense is 15. In that case the damage dealt is 20*4-15*2=80-30=50. With a variance of 20, the actual damage will be between 40-60 damage.
It would now be instructive to look at a standard spell: perhaps Water.
Code:
300 + a.mat * 2 - b.mdf * 2
  • mat is "magic attack", and mdf is "magic defense".
  • We can interpret the formula this way, "The damage dealt is 300 plus the user's magic attack multiplied by 2, minus the target's magic defense multiplied by 2."
    • Say the user's MAT is 20, and the enemy's MDF is 15. In that case the damage dealt is 300+(20*2-15*2)=300+(40-30)=300+10=310. With a variance of 20, the actual damage will be between 248-372 damage.
Aura Blade is up next. This skill is slightly more complex:
Code:
a.atk * 4 + a.mat * 3 - b.def - b.mdf
  • The skill description tells us, "Damage is based off user's ATK and MAT."
  • Translating into English, "The damage dealt equals the user's physical attack multiplied by 4 plus the user's magic attack multiplied by 3, minus the target's physical defense and magic defense."
    • Say the user's ATK is 25, MAT is 20, and the enemy's DEF is 30, MDF is 15. In that case the damage dealt is (25*4)+(20*3)-(30)-(15)=100+60-30-15=115. With a variance of 20, the actual damage will be between 92-138 damage. That's not very much - this skill is not necessarily good on all enemies.
"Okay, but what are all the parameters for a and b, besides atk, def, mat, and mdf?" Turns out that there are quite a lot of them. You will see that some have an ID number listed. This is important for using buffs in a formula, which I will get to towards the end.

  • level - current level, for actor only
  • hp - current HP, NOT max HP
  • mp - current MP, NOT max MP
  • tp - current TP

  • mhp - max HP
    • ID: 0
  • mmp - max MP
    • ID: 1
  • atk - attack stat
    • ID: 2
  • def - defense stat
    • ID: 3
  • mat - magic attack stat
    • ID: 4
  • mdf - magic defense stat
    • ID: 5
  • agi - agility stat
    • ID: 6
  • luk - luck stat
    • ID: 7

All of the following are decimal numbers. 1.0 converts to 100%. 0.5 converts to 50%. Etc. These values are generally not useful in damage formulas, but are certainly available if you want to get more creative.
  • hit - hit rate
  • eva - evasion rate
  • cri - critical rate
  • cev - critical evasion rate
  • mev - magic evasion rate
  • mrf - magic reflection rate
  • cnt - counter attack rate
  • hrg - HP regeneration rate
  • mrg - MP regeneration rate
  • trg - TP regeneration rate
  • tgr - target rate
  • grd - guard effect rate
  • rec - recovery effect rate
  • pha - pharmacology
  • mcr - MP cost rate
  • tcr - TP charge rate
  • pdr - physical damage rate
  • mdr - magical damage rate
  • fdr - floor damage rate
  • exr - experience rate

Exercises​
Of course, you don't "have" to do these exercises. However, I find that a good way to learn things like this is to actually try and make your own. If you're up to it, why not try creating...
  1. ...a defense-ignoring attack?​
  2. ...a healing spell dependent on the user's magic attack?​
  3. ...a resurrection spell that restores half the target's health? How about 5% of it? Or exactly 1 point?​
  4. ...a sword attack that drains life based on the user's attack and magic attack?​
  5. ...a simple damage dealing spell (e.g., Fireball)?​
  6. ...an attack based on the user's attack and defense? How about attack and agility?​
Spoiler: Solutions
  1. a.atk * 4
  2. a.mat * 10 OR 250 + a.mat OR b.mhp * 0.25 + a.mat, etc.
  3. Under Effects, a separate section from the formula, have the effect Remove State [Death] 100%. As for the formula: HP recover, with b.mhp / 2 for 50%, b.mhp / 20 for 5%, and just 1 for 1 health point.
    • See the end of the States section below for a different solution that utilizes only formulas and no effects.
  4. Set damage type to HP drain. Formula might look like a.atk * 4 + a.mat * 4 - b.def - b.mdf
  5. Something like a.mat * 4 - b.mdf * 2
  6. a.atk * 4 + a.def * 2 - b.def * 2 and a.atk * 4 + a.agi * 2 - b.def * 2
None of those exercises included any of the decimal parameters. You generally will not need them.

Everything included in the default VX Ace skill list has been covered by everything before this point, and more besides. So at this point we are finished with the basics and can proceed to the more complex formula functions!


Frog Drop: Using Level, Variables, and "If-Else"​
In Final Fantasy 9, a minigame exists whereby the player catches frogs. A character has a skill, called "Frog Drop", that increases in power directly dependent on the number of frogs caught and the level of the character, as follows:
Code:
Damage = Quina's level * Number of frogs caught
(It is supposed to drop all frogs ever caught onto the target.) This is quite easy to replicate in RM and is instructive for the tutorial, as it will let us examine variables and "if-else". We already learned how to get the character's level:
Code:
a.level
In RM, the best way to replicate the frogs caught is via a variable. Variables can actually be used in damage formulas, to great effect! Say Variable 1 is "Frogs caught". Then the damage formula would be:
Code:
a.level * v[1]
Quite simple, really. Now, in FF9, if no frogs are caught, the skill deals 1 damage. But as our formula stands, it would deal 0 damage (if v[1] is 0, then we multiply by 0 and get 0). How can we correct this? With an "if-else" statement, we can check to see if frogs have been caught. If not, 1 damage is dealt. Behold the following:
Code:
v[1] > 0 ? a.level * v[1] : 1
What does that mean? The syntax is as follows:
Code:
statement ? formula1 : formula2
Formula1 is used if statement is true. Formula2 is used otherwise. So what we are doing is saying, "Is Variable 1 greater than (>) 0? If so, then we're using the ordinary frog drop formula. Otherwise, Variable 1 must be 0. If it's 0, we'll just do 1 damage."
  • > - greater than
  • < - less than
  • >= - greater than or equal to
  • <= - less than or equal to
  • === - equal to
  • && - and
  • || - or
  • !== - not equal to
  • ! - not

States and Multiple Lines​
By far one of the most powerful and cool things you can do with damage formulas is state checks. Say you have a state, Poison. Maybe an attack deals extra damage if the target is poisoned. In Pokemon, the skill Venoshock does exactly this:
The user drenches the target in a special poisonous liquid. Its power is doubled if the target is poisoned.
We can replicate this in RPG Maker through an if-else statement, using the ternary operator, ?.
MV/Z:
Code:
b.isStateAffected(2) ? 2 * (a.atk * 4 - b.def * 2) : a.atk * 4 - b.def * 2
VX Ace:
Code:
b.state?(2) ? 2 * (a.atk * 4 - b.def * 2) : a.atk * 4 - b.def * 2
  • This formula assumes that Poison is state 2. Change 2 accordingly if necessary.
  • First, we just check to see if the target is poisoned (b.isStateAffected(2)
    • If the target is poisoned, it deals the normal damage formula, but doubled (2 * (a.atk * 4 - b.def * 2)).
    • Otherwise, it just does the normal formula (a.atk * 4 - b.def * 2).
Here are the rest of the state formulas. Make sure to place either a. or b. before each one.
  • MV/Z: isDeathStateAffected() - is the target dead? Returns a boolean (true or false)​
    • VX Ace: death_state?()​
  • MV/Z: resetStateCounts(ID) - refreshes how long a state will last for the battler. Does not return a value​
    • If trying to replicate Venoshock, you could add this line so that the target not only takes extra damage, the poison is also renewed on them.​
    • VX Ace: reset_state_count​
  • MV/Z: updateStateTurns() - shortens all states on battler by 1 turn. Does not return a value​
    • VX Ace: update_state_turns​
  • MV/Z: addState(ID) - adds state. Does not return a value​
    • VX Ace: add_state(ID)​
  • MV/Z: isStateAddable(ID) - can state be added? Returns a boolean​
    • VX Ace: state_addable?(ID)​
  • MV/Z: removeState(ID) - remove state. Does not return a value​
    • VX Ace: remove_state(ID)​
  • states.length - checks how many states are on the battler. Returns an integer.​
    • b.states.length * 500 would deal 500 damage for each state on the target.​
Semicolons are used to separate multiple lines of formula code. Let's revisit our Resurrection spell.
Code:
b.mhp / 2
We can change this as follows:
MV/Z:
Code:
b.removeState(1); b.mhp / 2
VX Ace:
Code:
b.remove_state(1); b.mhp / 2
Voila! The target's death state is removed and they are healed to 50% HP. Here, we can remove the death heal effect from the Effects section. This will let us combine everything in the damage formula, which I would argue is better.


Two Important Notes on Extra Lines​
If you have multiple lines of formula, only the last line will actually do damage! So if you have the following:
Code:
a.atk * 4 - b.def * 2; a.mat * 4 - b.mdf * 2
Only the a.mat * 4 - b.mdf * 2 will be computed as damage. So make sure that any prior lines you have are not damage dealing (in the example with the death state removal, that's fine, since no damage is dealt or healed).


In addition, if you want to write something that does not deal damage in a formula, you must also write another line with a 0 in it. What I mean is, you cannot simply write something like b.removeState(1) and be done. You must also include a 0, like so: b.removeState(1); 0.

Other Things​
  • HP and MP can be directly controlled with the following formulas, replacing x with the appropriate number or formula:
    • MV/Z: gainHp(x)
      • VX Ace: hp += x
    • MV/Z: gainMp(x)
      • VX Ace: mp += x
    • MV/Z: gainTp(x)
      • VX Ace: tp += x
    • MV/Z: setHp(x)
      • VX Ace: change_hp(x)
    • MV/Z: setMp(x)
      • VX Ace: change_mp(x)
    • MV/Z: setTp(x)
      • VX Ace: change_tp(x)
    • MV/Z: clearTp()
      • VX Ace: clear_tp
  • To obtain a random number, you can use three different functions:​
    • MV/Z: Math.random() - returns a random number between 0 and 1​
      • VX Ace: rand​
    • MV/Z: Math.floor(Math.random()*max)+min - returns a random number between min and max​
    • MV/Z: Math.randomInt(max)+min - returns a random number between min and max​
  • There may come a time when you want a skill to have different effects depending on whether the user/target is an actor or an enemy. In these cases you can use isActor() and isEnemy() (with an a. or b. before each). They return booleans (true-false) and can be used in if-else statements.
    • VX Ace: is_actor() and is_enemy()
  • Buffs and debuffs can be controlled via formulas. Each stat has a specific parameter ID. I listed those earlier in this guide. Be sure to include a damage line at the end if you use these, even if it is just a 0!
    • addBuff(ID, turns)
      • VX Ace: add_buff(ID, turns)
    • addDebuff(ID, turns)
      • VX Ace: add_debuff(ID, turns)
    • removeBuff(ID)
      • VX Ace: remove_buff(ID, turns)
    • removeDebuff(ID)
      • VX Ace: remove_buff(ID, turns)
    • removeAllBuffs()
      • VX Ace: clear_buffs
  • You can also use the function consumeItem($dataItems[ID]) to make a skill force the target to use an item. A skill might force the target to consume a potion, for example. Be sure to include a damage line.
    • VX Ace: consume_item(ID)
Conclusion​
That's all the basics you need to know. For help with specific skills, feel free to ask in this thread. I'll respond when I have time, and other helpful community members probably can too 🙂. Also, if you see any errors in the the post, please call me out on them so I can correct them.
I placed this tutorial in Non-Maker Specific because it has worked with MV and VX Ace.
b.isStateAffected(2)
This does not go well. Because there is no isStateAffected(x) in VXAce. Mind your word next time or it should go to MV/Z tutorial to avoid confusion 😉

Anyway I might as well as make a dictionary for MV to VXA terms here
  • isStateAffected(id) --> state?(id)
  • isDeathStateAffected() --> death_state?
  • resetStateCounts--> reset_state_count
  • updateStateTurns --> update_state_turns
  • addState(id) --> add_state(id)
  • removeState(id) --> remove_state(id)
  • isStateAddable(id) --> state_addable?(id)
  • gainHp(value) --> hp += value
  • setHp(hp) --> change_hp(value)
  • clearTp --> clear_tp
  • addBuff --> add_buff
  • addDebuff --> add_debuff
  • removeBuff/removeDebuff --> remove_buff (there is no remove debuff in vxa, it uses universal remove_buff)
  • removeAllBuffs --> clear_buffs
  • Math.random() --> rand
  • consumeItem(item) --> consume_item(item)
I think you should also add a warning about what including mechanics in your damage formulas might do, as is mentioned in this article by Yanfly. It's not gospel or anything, but people reading your tutorial should definitely be made aware of possible issues.
I think you should also add a warning about what including mechanics in your damage formulas might do, as is mentioned in this article by Yanfly. It's not gospel or anything, but people reading your tutorial should definitely be made aware of possible issues.
Did you mean, this article?

I did mention it only becomes an issue if you're using auto-battle.
Did you mean, this article?

I did mention it only becomes an issue if you're using auto-battle.
Must've grabbed the wrong link by mistake. Fixed now.

Anyway, it still needs to be mentioned in the tutorial in case someone is using auto-battle.
This is super helpful. Any chance someone could show an example of a formula for a skill that targets all allies and foes, but has different effects on each side? An example would be the Phoenix summon in Final Fantasy, which simultaneously damages enemies and revives allies in a single use.
This does not go well. Because there is no isStateAffected(x) in VXAce. Mind your word next time or it should go to MV/Z tutorial to avoid confusion 😉

Anyway I might as well as make a dictionary for MV to VXA terms here
  • isStateAffected(id) --> state?(id)
  • isDeathStateAffected() --> death_state?
  • resetStateCounts--> reset_state_count
  • updateStateTurns --> update_state_turns
  • addState(id) --> add_state(id)
  • removeState(id) --> remove_state(id)
  • isStateAddable(id) --> state_addable?(id)
  • gainHp(value) --> hp += value
  • setHp(hp) --> change_hp(value)
  • clearTp --> clear_tp
  • addBuff --> add_buff
  • addDebuff --> add_debuff
  • removeBuff/removeDebuff --> remove_buff (there is no remove debuff in vxa, it uses universal remove_buff)
  • removeAllBuffs --> clear_buffs
  • Math.random() --> rand
  • consumeItem(item) --> consume_item(item)
Thanks so much! I updated the main thread.
This is super helpful. Any chance someone could show an example of a formula for a skill that targets all allies and foes, but has different effects on each side? An example would be the Phoenix summon in Final Fantasy, which simultaneously damages enemies and revives allies in a single use.

The trick with this is that when you select what the skill does, you're choosing either damage or healing, so that's what the game makes your value from the damage formula do. You can use functions to force it do the other thing, but you're going to get a damage/healing popup of 0 for one team, which could look weird.

So the slickest way to handle this is to use action sequences and program the effect in per actor outside of the damage formula. But you could achieve this effect using this formula (as a damage skill):
Code:
if (b.isActor()) {b.gainHp(100); 0;} else 100
That will heal actors for 100 (and deal them 0 damage) and do 100 damage to enemy battlers.
The trick with this is that when you select what the skill does, you're choosing either damage or healing, so that's what the game makes your value from the damage formula do. You can use functions to force it do the other thing, but you're going to get a damage/healing popup of 0 for one team, which could look weird.

So the slickest way to handle this is to use action sequences and program the effect in per actor outside of the damage formula. But you could achieve this effect using this formula (as a damage skill):
Code:
if (b.isActor()) {b.gainHp(100); 0;} else 100
That will heal actors for 100 (and deal them 0 damage) and do 100 damage to enemy battlers.
And if you want to use the ternary operator, which I prefer:
Code:
b.isActor() ? (b.gainHp(100); 0) : 100
And in Ace:
Code:
b.is_actor() ? (b.hp += 100; 0) : 100
I added a credit for @TheoAllen to the main post for his very helpful post
The trick with this is that when you select what the skill does, you're choosing either damage or healing, so that's what the game makes your value from the damage formula do. You can use functions to force it do the other thing, but you're going to get a damage/healing popup of 0 for one team, which could look weird.

So the slickest way to handle this is to use action sequences and program the effect in per actor outside of the damage formula. But you could achieve this effect using this formula (as a damage skill):
Code:
if (b.isActor()) {b.gainHp(100); 0;} else 100
That will heal actors for 100 (and deal them 0 damage) and do 100 damage to enemy battlers.

Ah, that makes sense. Thanks for sharing.
And if you want to use the ternary operator, which I prefer:
Code:
b.isActor() ? (b.gainHp(100); 0) : 100
Will that work, with parentheses? That's why I didn't write it that way to begin with - the operator takes a single code statement in each portion. I could see maybe braces, as that's their purpose, to create code blocks, but I'm surprised that parentheses would also do that.

If so, good to know.
Will that work, with parentheses?
No, it won't.
To test that, is quite simple, just press F12 on your browser, and see.

Screenshot_1321.jpg


However, JS has a comma operator.
If you use commas on parenthesis, it will return the last value in the parenthesis.
No, it won't.
I thought not, but I always allow that other people may know more than I do 😉
However, JS has a comma operator.
If you use commas on parenthesis, it will return the last value in the parenthesis.
It's funny, I just explained commas in a thread the other day, but I didn't think to apply it here (I just use them so infrequently that I forget).

In that case, I shouldn't think the parentheses are necessary at all.
@ATT_Turan @TheoAllen I have tested using parenthesis in a ternary operator, and it has worked, e.g., with b.isStateAffected(2) ? 2 * (a.atk * 4 - b.def * 2) : a.atk * 4 - b.def * 2. The issue appears to be the presence of multiple lines with a semicolon, not the parenthesis.

Thank you for the correction! I haven't tried it with braces, but that would plausibly work.
@ATT_Turan @TheoAllen I have tested using parenthesis in a ternary operator, and it has worked, e.g., with b.isStateAffected(2) ? 2 * (a.atk * 4 - b.def * 2) : a.atk * 4 - b.def * 2.
No one has said that the mere presence of parentheses will cause code to not work 🙂 But the purpose of parentheses (outside of listing arguments for a function) is to group operations, which is what you're doing in your example - I would have expected that to work perfectly.

My question above was you using parentheses to combine multiple statements into a field that only accepts one, which is typically what braces are for, or the comma operator.
The issue appears to be the presence of multiple lines with a semicolon, not the parenthesis.
Correct.
No one has said that the mere presence of parentheses will cause code to not work 🙂 But the purpose of parentheses (outside of listing arguments for a function) is to group operations, which is what you're doing in your example - I would have expected that to work perfectly.

My question above was you using parentheses to combine multiple statements into a field that only accepts one, which is typically what braces are for, or the comma operator.

Correct.
Okay, I misunderstood your original post. Thank you for the clarification 🙂
Bumping this up to the top.
This tutorial is wonderful to help understand the formulas better! Very simple and straight forward. I like to make up my own skills but it can be a little intimidating when not fully understanding it, so this really helps!
This tutorial is wonderful to help understand the formulas better! Very simple and straight forward. I like to make up my own skills but it can be a little intimidating when not fully understanding it, so this really helps!
Thanks for the positive feedback 🙂. Anything you thought I could have improved on/explained better?

Original thread: https://forums.rpgmakerweb.com/threads/a-guide-to-damage-formulas.145148/