Functions for formulas

Hello, and thank you ahead of time for your help!


I'm hoping I use the correct terms here as I'm still learning. But I'm wondering if I can define a formula as a function or variable, then call that function as part of the damage formula. 


The reason is this, my damage formula is long, and I reference it with many skills over and over. 


((a.level*a.level*a.atk)/256)+(a.atk*2)+(a.def*2)


As it stands, I copy and paste it into a skill, then add a modifier. For example "Heavy Slash" might be:


(((a.level*a.level*a.atk)/256)+(a.atk*2)+(a.def*2)*1.25)


This isn't a huge deal, but let's say late in the game dev, I find that this formula isn't working. I'll have to go back and find every point I reference with this formula, and hope I update them all. Again, please forgive me if I'm using the wrong terminology, but it would be much easier if I could define the "standard damage formula". Something like:


var standard_attack = ((a.level*a.level*a.atk)/256)+(a.atk*2)+(a.def*2)


Then, in the forumla bar of the skills, I could do something like:


standard_attack * 1.25


Does this functionality exist, or is there a plugin that could allow it?


I hope this was clear. Thanks!
@jojarth


Well, you can easily do it with either declaring a global variable or using a gameVariable.


At start of the game:


Control variables [your_variable_id] : script : " ((a.level*a.level*a.atk)/256)+(a.atk*2)+(a.def*2)" (with the quotes).


And in every damage formula that uses the formula: eval($gameVariables.value(your_variable_id))


Or just declare in a script call basic_atk_formula = " ((a.level*a.level*a.atk)/256)+(a.atk*2)+(a.def*2)". Then you'll be able to refer to it anywhere. You'll still have to declare it as a string and use eval in the formula box.


I'd go against creating new globals if you don't know precisely what you're doing, and use the provided game variables.


Eval is a very powerful fuction that can allow you to postpone evaluation of a script provided as a string until the moment you want to. 


It's perfect for what you want to do right now.


Just be sure never to let your player input the code to be evaled.
Awesome! This is exactly what I was looking for. 


Thank you so much! I really appreciate it! Have a great weekend!

Original thread: https://forums.rpgmakerweb.com/threads/functions-for-formulas.64512/