# Damage Formulas 101

#### Nox_Aeternae

##### Villager
@Trihan I wasn't super sure if the && function would be appropriate there and it seems it wasn't. I'm pretty dumb when it comes to math and formulas so, I might add a few pages to this thread as I try to learn how to use this function.Please bear with me

I'm having trouble with another formula right now. I'm trying to nest if conditions but clearly I'm doing something wrong.

if (b.actorId() === 1) 500; else; if (b.actorId() === 2) 0-100; else 200

What I want this to do is: it checks if the item is being used on actor 1 and if true heals them for 500. If it returns false, then it checks if it's being used on actor 2 in which case it deals 100 damage. If none of those are correct and the item is being used a different character, it heals them for 200.

#### Trihan

##### Speedy Scripter
The only thing you're missing is that there shouldn't be a ; between your else and the next if.

#### Nox_Aeternae

##### Villager
The only thing you're missing is that there shouldn't be a ; between your else and the next if.

Ah, I actually tried it before without the semicolon but I didn't have the parentheses around the b.actorId() === 1 / 2 when I did. I tried it as you said and the conditionals work, but the damage doesn't apply for actor 2. I'll try writing that part with the gainHp function.

Edit: Using the gainHp function worked for the negative heal. Thanks for the help!

For anyone else that might want to do something similar, the formula that ended up working perfectly is:

if (b.actorId() === 1) 500; else if (b.actorId() === 2) b.gainHp(-100); else 200

Last edited:
Bex

#### Solar_Flare

##### Veteran
To expand on what Andar said, when you used && in your first formula, you actually turned it into a *boolean condition*.

a.mhp* 20 / 100 && a.gainHp(0 - a.mhp* 20 / 100)

This would evaluate to *true* if both sides are truthy, and *false* if either side is falsy.

Because a.hp * 20 / 100 just returns itself, that always returns truthy. But the gainHp function doesn't return a value, so it will never evaluate to a truthy value in a condition.

Therefore, your formula was evaluating to *false*, but booleans aren't numbers so it was unable to deal any damage using it.
This is actually incorrect. The `&&` and `||` operators in JavaScript do not coerce their arguments to booleans - `||` returns the first argument unchanged if it is truthy, otherwise it returns the second argument. Similarly, `&&` returns the first argument unchanged if it is falsy, otherwise it returns the second argument.

#### Trihan

##### Speedy Scripter
This is actually incorrect. The `&&` and `||` operators in JavaScript do not coerce their arguments to booleans - `||` returns the first argument unchanged if it is truthy, otherwise it returns the second argument. Similarly, `&&` returns the first argument unchanged if it is falsy, otherwise it returns the second argument.
Oops, you're right. Thanks for the correction!

#### Arctica

##### Veteran
So.. after 3 weeks of banging my head on the desk enough times that the desk broke, I came up with a half of a formula that I'm satisfied with..
Code:
``Math.pow(a.atk, 1.6) * 1.7``

To keeps damage low in the low level range than slowly climbs higher.

The big problem I'm having is factoring in defense. I guess I need some ideas with that. Maximum def is 255. I've tried reducing damage as a percentage like
Code:
``DMG * (1 - (DEF / 255))``
but I donno, scale badly. There should be a noticeable difference in defensive power between characters(the main character has the lowest for example, she starts with 9 DEF, and her 'max' is 85. A secondary character starts with 15 and has a 'max' of 98)

#### Drakkonis

##### Veteran
So.. after 3 weeks of banging my head on the desk enough times that the desk broke, I came up with a half of a formula that I'm satisfied with..
Code:
``Math.pow(a.atk, 1.6) * 1.7``

To keeps damage low in the low level range than slowly climbs higher.

The big problem I'm having is factoring in defense. I guess I need some ideas with that. Maximum def is 255. I've tried reducing damage as a percentage like
Code:
``DMG * (1 - (DEF / 255))``
but I donno, scale badly. There should be a noticeable difference in defensive power between characters(the main character has the lowest for example, she starts with 9 DEF, and her 'max' is 85. A secondary character starts with 15 and has a 'max' of 98)
I suppose that depends on what kind of scale you're hoping for. From your example, someone with max defense of 255 would negate all damage. I assume that's what you want. Your secondary character at their "max" would only take 62% damage. But both characters at the start wouldn't have much difference in damage reduction between them, maybe 1%. And at low damage levels that difference wouldn't even be distinguishable from normal damage variance.

If you want a noticeable difference in damage reduction between the two at lower levels, you may want to include some modulation based on their levels, so that the differences have more impact when they're lower level, with that "advantage" shrinking and effectively disappearing at higher levels.

#### Arctica

##### Veteran
Well I originally went with 255/400, which would've capped it to 63% but the lower bound also shrinks even more. I'm not very good with math. I just play around with adding/sub/div/multiply and exponents at their very basic.

#### ATT_Turan

##### Forewarner of the Black Wind
I'm not very good with math. I just play around with adding/sub/div/multiply and exponents at their very basic.
Then why not use the various damage threads as resources?

I'm a fan of this damage formula, which has a full explanation a few posts later.

#### Arctica

##### Veteran
Then why not use the various damage threads as resources?

I'm a fan of this damage formula, which has a full explanation a few posts later.
That I will experiment with yes.

#### Nox_Aeternae

##### Villager
I'm trying to make a rudimentary combo system using variables but I can't for the life of me get this formula to work:

if (\$gameVariables.value(28) === 1) \$gameVariables.setValue(28,2) a.atk * 4; else \$gameVariables.setValue(28,0) a.atk

I tried moving the semicolons and parenthesis around but couldn't figure out what was wrong. I'd appreciate any help.

#### Solar_Flare

##### Veteran
You're missing a comma before `a.atk` (both of them).

#### Nox_Aeternae

##### Villager
You're missing a comma before `a.atk` (both of them).

That worked perfectly, thanks. I didn't know you could even use commas in there. I think the original post doesn't mention them. So, they're used to separate different calculations?

#### ATT_Turan

##### Forewarner of the Black Wind
I didn't know you could even use commas in there...So, they're used to separate different calculations?
Technically, they're used to put multiple statements into a space that only allows one.

It works here just fine. The more typical notation to use in this situation (where you're taking normally-written code and condensing it onto a single line) would be:
Code:
``if (\$gameVariables.value(28) === 1) {\$gameVariables.setValue(28,2); a.atk * 4;}``

#### Solar_Flare

##### Veteran
Yeah, basically comma and semicolon are both "statement separators". The semicolon is a stronger separator, so if you had used that instead of a comma in your example, you would get an error because the "else" is now invalid, which is why ATT_Turan added braces to correct for that.

#### Nox_Aeternae

##### Villager
It's me again with more questions. I can not for the life of me figure out why this formula doesn't work:

(&gameSwitches.setValue(11) === true) ; (a.gainMp(3))

The other question I have is that I know how to set variables in a damage formula but I don't know how to increase them. I'm trying to create a skill that does x times the damage of a variable and the variable is increased every time the skill is used.

#### ThreeSixNine

##### Veteran
@NoxAeternae Try:
\$gameSwitches.setValue(11, true); a.gainMp(3);

I think there's a few issues with the way you have it now.
The leading symbol should be '\$' not '&'.
The setValue() function takes 2 arguments, the switch ID and the value( true or false).
I'm making the assumption that you're trying to turn on switch 11 when the skill is used. If you simply want to check it switch 11 is turned on, you'll need to change it to:
\$gameSwitches.value(11) === true;

The value() function takes one argument, the switch ID, and returns true or false, so it can be compared using an equality operator.

Also, to increase the variable try:
\$gameVariables.setValue(\$gameVariables.value(ID), \$gameVariables.value(ID) + 1);

Where ID is the ID of the variable you want to increase.

#### ATT_Turan

##### Forewarner of the Black Wind
It's me again with more questions. I can not for the life of me figure out why this formula doesn't work:
(&gameSwitches.setValue(11) === true) ; (a.gainMp(3))
Most of what ThreeSixNine said is spot on. One thing to emphasize, is "===" is always conditional. It's asking "is this equal?" (incidentally, it's also a strict equality which in RPG Maker is rarely necessary. You'll get the same results from just "=="). Any time you want to make something equal to something else, it's only one "=". So aside from it being the wrong syntax for the setValue() method, that's a fundamental error to keep in mind.

Also, they're not hurting anything, but I'd encourage you to get rid of all of the extra parentheses in your code. They can make it more confusing to read in situations where you actually do need them for function calls or proper grouping.

Try:...
I think there's a few issues with the way you have it now...
This is all perfect.
I'm making the assumption that you're trying to turn on switch 11 when the skill is used. If you simply want to check it switch 11 is turned on, you'll need to change it to:
\$gameSwitches.value(11) === true;
This is leaving a bit out, for my preferences That's a valid conditional, but it would have to be inside of an if statement, or part of a ternary operation, and the rest of the formula would need to be rewritten to accommodate it. So if you were actually trying to change the formula based on whether the switch was true or false, you'd have to rewrite much more than just this.

I know that you know this, ThreeSixNine, but Nox is clearly at a beginning level.

The other question I have is that I know how to set variables in a damage formula but I don't know how to increase them. I'm trying to create a skill that does x times the damage of a variable and the variable is increased every time the skill is used.
Also, to increase the variable try:
\$gameVariables.setValue(\$gameVariables.value(ID), \$gameVariables.value(ID) + 1);
This is also correct - however, the damage formula box makes this even easier, because you can use the v[x] shorthand. So you'd just do `v[ID]++` to increase it by one. Therefore, the formula for your skill that does the damage then increases the variable would be
Code:
``v[ID]++ * x``
where ID is the ID of the variable and x is how much you want to multiply it by.

Last edited:

#### Nox_Aeternae

##### Villager
Thanks a lot both of you

@ThreeSixNine
The & symbol was a typo, I have it as \$ in-game.

I thought (\$gameSwitches.setValue(11) === true) would set the value of the switch. I've used a function with two arguments before when setting variables, but I didn't know the argument could be anything other than a number so doing \$gameSwitches.setValue(11, true) was completely new to me.

@ATT_Turan

You're saying = is used to make something equal to something else. Does that mean if I wrote my original formula as \$gameSwitches.setValue(11) = true, it would work? Or does the function .setValue necessitate me to write it as you two have suggested?

As for the unnecessary parenthesis, I'm still not sure where they're needed and where they're not. I didn't have them in this formula at the start but added them in later in case they were needed.

Code:
``v[ID]++ * x``
is incredibly simple and short so thanks for letting me know I can use it! But to my inexperienced eye, it seems like it would first increase the variable and then multiply it and return a damage value. I'm assuming that's not the case? Also, is there a reason for using multiple + symbols and how would I change it if I wanted to increase the variable by more than 1?

Edit: I tried your methods out and the first formula with the mana gain worked perfectly. As for the second formula, I tried the v[ID]++ * x method and it didn't work at first because I didn't have that variable assigned a value. It worked like a charm when I assigned a value to it before the battle. But if I didn't want to do that, I'm assuming ThreeSixNine's formula would work even if the variable didn't have a value beforehand. Am I correct in that assumption or would both methods require the variable to be assigned a value beforehand?

Last edited:

#### ATT_Turan

##### Forewarner of the Black Wind
I thought (\$gameSwitches.setValue(11) === true) would set the value of the switch. I've used a function with two arguments before when setting variables, but I didn't know the argument could be anything other than a number so doing \$gameSwitches.setValue(11, true) was completely new to me.
Where did you even find out about `\$gameSwitches.setValue()`? I can't imagine where you would've seen it that didn't include the correct syntax.
You're saying = is used to make something equal to something else. Does that mean if I wrote my original formula as \$gameSwitches.setValue(11) = true, it would work?
No. You really need to do some basic JavaScript tutorials if you want to use script calls like this - I suggest w3schools, starting with the JS statements chapter.

You can never set a function equal to something else, a function is always returning a value.
Or does the function .setValue necessitate me to write it as you two have suggested?
You also can't just choose to call a function with a different number of arguments. setValue() takes an ID and a boolean, it won't work if you don't supply them.
As for the unnecessary parenthesis, I'm still not sure where they're needed and where they're not.
They're not. There are only two times in code when you need to use parentheses.
1 - You're calling a function. Functions always have () at the end, and if there are any arguments they're listed inside the parentheses. You did this correctly at the end of setValue() and gainMp(), even if the contents weren't always correct. There's no reason to encase the entire code statement in additional parentheses.

2 - You're grouping mathematical functions to specify what happens first. This works exactly the same way as math class in school.
Code:
``v[ID]++ * x``
is incredibly simple and short so thanks for letting me know I can use it! But to my inexperienced eye, it seems like it would first increase the variable and then multiply it and return a damage value.
Again...look up some tutorials. This is a basic operator you'll probably see in your very first lesson. ++ adds 1 to the value of the variable, after the code statement is executed.
Also, is there a reason for using multiple + symbols
Because that's the operator. Saying `variable+ * x` wouldn't mean anything.
how would I change it if I wanted to increase the variable by more than 1?
Then the shorthand wouldn't work and you'd have to do it as a separate statement. For example:
Code:
``var dam = v[ID] * x; v[ID]+=3; dam;``
where ID is the variable and x is how much you want to multiply it by, as above.

### Latest Profile Posts

Messing around with fragment shaders (filters). This thing is HUGE and I'm surprised almost no one makes good use of. Even the number of plugins that do this stuff are limited.

There's 7 main areas in the game that are all connected. This train get's you from an underground lab to the center of someone's subconscious. inside the train you can talk to some of the passengers. but going to the top of it, there's battles.
Speedrunning all character portraits just so they all have the same quality. Not sure if this was a good idea, but now I'm almost done.
We now have a gameplay video!
That is a cute Link picture, reminds me of the Samurai Jack artstyle.