Doing math in events

Okay, so I have a custom variable that takes the place of the attack parameter, now what I want to do is instead of having a static number for the variable, I want to make a common event that does math similar to a damage formula so I can display that number for the attack variable.

So normally you create a weapon that has a set attack.


Example: attack 10

Okay what I want to do is, find a way to do the same math that’s done in the damage formulas in a common event.

That way, let’s say the damage formula is something like

a.atk * a.mat / 10 = “damage”

I want to be able to tie That “damage” to a variable so I can display it in menu.
So you want it to display the final damage output?

Based on 2 things, the skill in use then the targets stats?


And will this be calculated during battle?

rather then the damage formula why not just get the damage after the skill is done and store that in a variable?
If you want to do the same math as in a damage formula in a common event you need to use some javascript.

I'd suggest to use the "eval" method, after setting up some context.

e.g.


Code:
let a = user;
let b = target;
let tmp = eval("your formula");
$gameVariables.setValue(7, tmp); //Result is stored in variable 7

Of course the formula can be retrieved from a skill or an item.

Code:
let formula_skill = $dataSkills[42].damage.formula; //Retrieve formula of skill 42

I cannot help you with defining the "user" and "target" without knowing from where you will call this common event.

My two cents.
If you want to do the same math as in a damage formula in a common event you need to use some javascript.

I'd suggest to use the "eval" method, after setting up some context.

e.g.


Code:
let a = user;
let b = target;
let tmp = eval("your formula");
$gameVariables.setValue(7, tmp); //Result is stored in variable 7

Of course the formula can be retrieved from a skill or an item.

Code:
let formula_skill = $dataSkills[42].damage.formula; //Retrieve formula of skill 42

I cannot help you with defining the "user" and "target" without knowing from where you will call this common event.

My two cents.
So this formula would be a bit different, the user would be actor 1 and there would be no target, this is for displaying damage in the equip/status menu so player will be able to see damage more accurately, I have a skill system in the game, it has stats like strength, charisma etc. and skills like ranged weapons, melee weapons, etc. so a formula would look like.

(a.atk * a.str * a.meleeweapons) - b.def / 10 = “damage in battle”

For the purpose of display in menus it would look something like:

(a.atk * a.str * a.meleeweapons) / 10 = “damage listed in menu”

I want to store it in a variable so I can have it appear in the menu as a custom parameter. I just need to know how to write out the math/scripting, I have everything else figured out.

This common event would be called while exploring the world and after you equip a weapon for example.
What I would probably do in that case is something like this :

Code:
let a = $gameActors.actor(1); //User of the skill is actor 1

let formula = "(a.atk * a.str * a.meleeweapons) / 10" ;  
let tmp = Math.floor(eval(formula)); //Rounded down

$gameVariables.setValue(7, tmp); //Result is stored in variable 7

It's not tested (not being at home right now).
I guess that you already have some plugins that make sure that "a.str" and "a.meleeweapons" are valid.

Original thread: https://forums.rpgmakerweb.com/threads/doing-math-in-events.174384/