[RMMZ] Is it possible to use multiply your damage based on a variable?

Looking over the damage formula guide, I saw that you can indeed use variables to determine damage. However, what I am trying to do here is something along the lines of (damage formula) x (variable/100 + 1) in order to get the final damage of my attack, since I want to have a variable that server as a percentage boost to the overall damage of certain attacks

I can technically do this using the method presented in the damage formula guide, but I would need to make a case for every single value of the variable from 1 to 100 (since that's where I will cap it off), which will be incredibly annoying
Wait, why would you need to make a case for every possible value of it?
Yes it is. Put "v[1]" in the formula to refer to variable 1.
Replace 1 with the id of the variable you want to use

The formula would then be:
Code:
(A.ATK * 4 - B.DEF * 2) * (V[1] / 100 + 1)
Yes it is. Put "v[1]" in the formula to refer to variable 1.
Replace 1 with the id of the variable you want to use

The formula would then be:
Code:
(A.ATK * 4 - B.DEF * 2) * (V[1] / 100 + 1)
I see, thanks a lot

Wait, why would you need to make a case for every possible value of it?
Because I had no idea that I can just use a variable like that
Yes it is. Put "v[1]" in the formula to refer to variable 1.
Replace 1 with the id of the variable you want to use

The formula would then be:
Code:
(A.ATK * 4 - B.DEF * 2) * (V[1] / 100 + 1)
Just figured I'd add in a little note about something to keep in mind when using damage formulas like this: make sure that the variable will definitely have a value stored in it before the first time the damage formula will get evaluated.

When you access a variable through that notation, it accesses the raw variable data, meaning that there is no protection against trying to access undefined variables. Trying to read an undefined variable will cause the damage formula to bug out, and the skill will wind up doing 0 damage.

You could have an event set the initial value of the variable at some point before the skill is accessible. However, during playtesting, you might be in a situation where you've placed the player's starting position at some point after that, and then the variable won't get initialized properly. Doing a direct Battle Test would also bypass the initialization of the variable.

So it'd probably be best to use a plugin that executes a common event any time a new game is started, and have that common event initialize any variables such as this one. Alternatively, you could build some protection into the damage formula:
Code:
(A.ATK * 4 - B.DEF * 2) * ((V[1] || 0) / 100 + 1)
Just replace the 0 with whatever you consider to be an appropriate default value for the variable.
To echo that, during the game, events and switches will return 0 if a variable is not set. But, as I learned the hard way, referencing the variable directly doesn't inherit this behavior. So zero it out at the beginning of the game, and use troop events or chests or whatever for playtesting to set the variable.
Just figured I'd add in a little note about something to keep in mind when using damage formulas like this: make sure that the variable will definitely have a value stored in it before the first time the damage formula will get evaluated.
Ah, that won't be a problem. My general idea was to have "evil" skills which increase in power depending on how big your "evil actions" or whatever variable is, which is permanently increased depending on the choices you take in the story, and I wasn't planning on giving the player the option to decrease it
Just figured I'd add in a little note about something to keep in mind when using damage formulas like this: make sure that the variable will definitely have a value stored in it before the first time the damage formula will get evaluated.

When you access a variable through that notation, it accesses the raw variable data, meaning that there is no protection against trying to access undefined variables. Trying to read an undefined variable will cause the damage formula to bug out, and the skill will wind up doing 0 damage.

You could have an event set the initial value of the variable at some point before the skill is accessible. However, during playtesting, you might be in a situation where you've placed the player's starting position at some point after that, and then the variable won't get initialized properly. Doing a direct Battle Test would also bypass the initialization of the variable.

So it'd probably be best to use a plugin that executes a common event any time a new game is started, and have that common event initialize any variables such as this one. Alternatively, you could build some protection into the damage formula:
Code:
(A.ATK * 4 - B.DEF * 2) * ((V[1] || 0) / 100 + 1)
Just replace the 0 with whatever you consider to be an appropriate default value for the variable.

Why? Why need a default vallue in the variable? The OP actually did a good job with the "+1" in the formula, making sure the value in the variable is EXTRA on top of the default 100%. So when the value is 0, it will still give the base formula as aoutput.

I know why you say this, and I do understand the mistake. If the v[x] would have been behind the \, then a 0 would have been a problem (since you can't devide by 0). And without the +1 near the end, a 0 would result in no damage at all.

Maybe there is something I am completely overlooking here, but I never had a problem using unused variables in formula's like this. If no value is set, it would just return as 0. If this is wrong, then I have a lot (and I mean a lot) of script calls to edit. 😛
@JohnDoeNews - v[1] accesses $gameVariables._data directly. If variable 1 has not been set yet this playthrough, v[1] will return undefined. (v[1] || 0) returns 0 if v[1] is a falsy value, e.g. 0 or undefined. $gameVariables.value(1) (as used by event commands) does this safety check automatically.

Also, note that code, including the damage formula, is case-sensitive. Use lower-case here, e.g.

(a.atk * 4 - b.def * 2) * (v[1] / 100 + 1)

(Plugins can change any aspect of the core scripts.)
Why? Why need a default vallue in the variable? The OP actually did a good job with the "+1" in the formula, making sure the value in the variable is EXTRA on top of the default 100%. So when the value is 0, it will still give the base formula as aoutput.

I know why you say this, and I do understand the mistake. If the v[x] would have been behind the \, then a 0 would have been a problem (since you can't devide by 0). And without the +1 near the end, a 0 would result in no damage at all.

Maybe there is something I am completely overlooking here, but I never had a problem using unused variables in formula's like this. If no value is set, it would just return as 0. If this is wrong, then I have a lot (and I mean a lot) of script calls to edit. 😛
The issue is that the "v" identifier in the formula is a reference to $gameVariables._data, the raw values behind game variables. There is no sanity checking for value on this, so if you use an undefined variable in there and try to do a calculation on it, it will *always* return 0 for the formula as a whole because trying to do calculations on the value "undefined" will result in NaN.
Okay, cool. I pearned something today. 🙂

Original thread: https://forums.rpgmakerweb.com/threads/rmmz-is-it-possible-to-use-multiply-your-damage-based-on-a-variable.159112/