Core damage formula for all skills to use

Hey everyone, 


I have a question about managing skill formulae.  For my game, I'd like to tie many skills to the same basic formula, and have each skill modify that base damage with a skill power variable, kind of like Pokemon does (ex. Fire Blast has a power of around 120, making it stronger than Flamethrower at about power 90).  This would let me avoid copying and pasting the damage formula into each individual skill, and would also let me experiment with damage formulas a little bit by altering the root formula instead of each individual skill if I needed to tweak or balance the damage.  Has anyone heard of a good plugin or workaround that could solve this problem?


As a last thing, not every skill would use this formula, just the ones that inflict HP damage.


Thanks a ton!
You could use an eval. The eval function allows you to put strings to be executed as javascript code at a later point. It's perfect for this.


So your basic formula box for your damaging spells would be:


eval(v[1])


You'd just have to set variable 1 to your formula at the beginning of the game, using Control variables: script.


Ex:


"a.atk * 4 - b.def * 2"


For the default MV attack formula.


This would even allow you to change your base damage formula dynamically in game, by changing variable 1. Useful if you have a part of your game where you want physical attacks to deal less damage without changing parameters for instance.


To complete the pokemon analogy, using another variable you could have two basic damage formulas for special attack and attack based spells.
You could use an eval. The eval function allows you to put strings to be executed as javascript code at a later point. It's perfect for this.


So your basic formula box for your damaging spells would be:



eval(v[1])


You'd just have to set variable 1 to your formula at the beginning of the game, using Control variables: script.


Ex:



"a.atk * 4 - b.def * 2"


For the default MV attack formula.


This would even allow you to change your base damage formula dynamically in game, by changing variable 1. Useful if you have a part of your game where you want physical attacks to deal less damage without changing parameters for instance.


To complete the pokemon analogy, using another variable you could have two basic damage formulas for special attack and attack based spells.



Thanks, I just did a bit of testing, and I think that'll work great!  Just for learning purposes, when else might you use an eval?  I'm pretty new to the whole scripts and coding thing.
Or you could just make a new js file and make a damage function inside that takes the multiplier as a parameter then call it on the damage formula...


something like


var Damage = {}

Damage.formula = function(a,b,multiplier) {return multiplier*(a.atk*4 - b.def*2)};


then on your damage formula you just need to put


Damage.formula(a,b,multiplier) where multiplier will be your damage multiplier, so like 1.20 for 120%


or change Damage and formula to shorter names if you want to have shorter things to copy into your damage formula


Not sure in MV but in Ace, avoiding eval is a good thing since its slower and can have some consequences...
I'm no experienced programmer, so I can't really say anything on the eval function. Just that the security risk is non existent as long as you don't let the player run it's own code. It might be slower, but this is code that won't be run more than once a second at most, is it really important to optimize it?


The advantages of this solution are that it works in editor, and that you can change the formula in game for flexibility.


It can all be acheived with a small plugin, of course, but in my opinion it's not really worth it. I guess that if you have 10 different base damage formulas it can be worth it, to have them all in the same place. It's also less error prone, because you can overwrite the variables by accident mid game, and that can't happen with your definitions.


Eval is used throughout the engine, for instance the event command script, and for the damage formula.


You shouldn't really have a need for it as the developper. This is the only case I had to in order to make things work in editor.
I just presented an alternative to the eval one so that he will know all possible ways to do what he wants. It is up to him to use whatever method he prefers.

It's also less error prone, because you can overwrite the variables by accident mid game, and that can't happen with your definitions.

yeah, that's definitely a possible problem if the creator gets confused after using so many variables. I've seen quite a lot of threads over the years where people get problems and end up realizing it's because they ended up modifying the wrong variables or modifying a certain variable (when they shouldn't) at some point.
And I responded in kind, with pros and cons to both alternatives to allow him to make a more informed decision 🙂


I hadn't thought of creating a small plugin like that myself so your suggestion is quite helpful.


Cheers!
Thanks for the ideas.  That gives me a couple of good options to consider.
Take note though that it's untested so I'm not sure if it is actually working. but the idea should be correct. 🙂

Original thread: https://forums.rpgmakerweb.com/threads/core-damage-formula-for-all-skills-to-use.72562/