Damage Formula in Skill description.

Edit 2: 09.02.2025: Here is the current solution. So far i haven't encountered a problem. I use the VisuMZ_1_MessageCore plugin with its necessary requirements for it to work. The script is called via the Text Code Macro function in the plugin.

The code:
JavaScript:
// Checks if the script is run while player is in battle.
if (SceneManager._scene instanceof Scene_Battle)
{
    // Get the info of the current selected actor when in battle.
    a = BattleManager.actor();
    
    //Get the info of the selectes skill when in battle.
    item = SceneManager._scene._skillWindow.item();
}
/* This is needed when VisuMZ_4_SkillShop plugin is used. Without this the game crashes uposn trying to
   selcet a skill in the skill shop. */
else if (SceneManager._scene instanceof Scene_SkillShop)
{   
    // Displays "Not Available" insteas of the number for the "power" of the skill.
    return "Not Available"
}
// Is used when the scrip is run and player is not in battle.
else
{
    // Gets the info of the actor selected in the menu (the menu that opens when pressing ESC).
    a = $gameParty.menuActor();
    
    /* Gets the info of the skill currently selected in the menu (the menu that opens when pressing ESC).
    if it should stop working / not work try replace it with the one below. */
    item = SceneManager._scene.item();
    // item = SceneManager._scene._itemWIndow.item();
}
/* Checks if the variable "item" is set to "null". Primarily used to prevent crashing the game when a
   skill replaces the "attack command" while in battle */
if (item == null)
{
    // Sets the variable "item" to the id of the skill the is used for the "attack command" Is set as a number.
    item = a.attackSkillId();
    
    /* Sets the variable "skill_dmg" to the Formula in the damage section from the selected skill.
       The script call "$dataSkills[id]" is used since the variable "item" is a number. */
    const skill_dmg = $dataSkills[item].damage.formula;
    
    // Calculates the number of the formula from the selected skill
    let value = Math.max(eval(skill_dmg), 0);
    
    // Display the calculated number.
    return (isNaN(value) ? 0 : value);
}
// Is used when the variabel "item" is NOT set as "null".
else
{
    //Sets the variabel "skill_dmg" to the Formula in the damage section from the selected skill.
    const skill_dmg = item.damage.formula;
    
    // Calculates the number of the formula from the selected skill
    let value = Math.max(eval(skill_dmg), 0);
    
    // Display the calculated number.
    return (isNaN(value) ? 0 : value);
}


Edit 1 : 07.02.2025: Added thing and changed formatting for more clarity, hopefully

I'm trying to have the damage formulas of the skills displayed as a number in the description of the skill like Base Power: 123.
But I don't really know how to do it. I use the VisuStella Plugin, all core plugins plus some addition plugins one of them is the skill learn system and the skill shop system.

The goal is the have the "Base Power" of a skill displayed in battle, in the skill shop, int the skill menu when viewing the learned skill and in the skill learn menu.

I asked int the VisuStella discord and was told that this is doable with the message core macro function.

I'm struggling with the JS code. I looked at some older forum entries and at the MZ Scrip Call and Scrip Call References. it only helped somewhat.

I have some JS code but i'm stuck at it, because i don't know how to search for certain things and i don't know if the current code that i have is even correct.

Here are some examples of the damage formulas used by the skills:

((a.level*4)+(a.mmp*0.8)+(a.mat*1.1))

((a.level*1.0)+(a.atk*(1.01+(a.level/100))))*0.5

a.states().includes($dataStates[33]) ? ((a.level*3)+(a.mmp*0.7)+(a.mat*1.2))*1.2 : ((a.level*3)+(a.mmp*0.7)+(a.mat*1.0))

The damage calculation against enemy stats id done through the VisuStella Battle core -> damage settings -> damage style
Her is what i have.

JavaScript:
if (SceneManager.isSceneBattle()) //<- To check if in battle, Copied, slightly modified from the damage formula "Armor Scaling" from the battle core
{
    const a = BattleManager.actor(); //<- this is apparently how to get the selected actor in battle
}
else
{
    const a = $gameParty.menuActor(); //<- and this is how to get the selected actor outside of battle
}
let formula = $dataSkills[id].damage.formula // <- to my understanding this is how to get the damage formula of a skill
// according to the MZ Scrip Call References
let value = Math.max(eval(formula), 0); //<- Copied from the damage formula "Armor Scaling" from the battle core
return (isNaN(value) ? 0 : value); //<- Copied, slightly modified from the damage formula "Armor Scaling" from the battle core

Here is the code of the mentioned battle style Armor scaling.

JavaScript:
// Declare Constants
const user = this.subject();
const target = arguments[0];
const item = this.item();
const a = this.subject();
const b = target;
const v = $gameVariables._data;
const sign = [3, 4].includes(item.damage.type) ? -1 : 1;

// Replace Formula
let formula = item.damage.formula;
if (SceneManager.isSceneBattle() && !this.isCertainHit()) {
    const fmt = 'Math.max(this.applyArmorModifiers(b, %1), 1)';
    formula = formula.replace(/b.def/g, fmt.format('b.def'));
    formula = formula.replace(/b.mdf/g, fmt.format('b.mdf'));
    formula = formula.replace(/b.agi/g, fmt.format('b.agi'));
    formula = formula.replace(/b.luk/g, fmt.format('b.luk'));
}

// Calculate Damage
let value = Math.max(eval(formula), 0);

// Apply Defender's Defense Parameter
if (this.isDamage() && !this.isCertainHit()) {

    // Calculate Base Armor
    let armor = this.isPhysical() ? b.def : b.mdf;
    armor = this.applyArmorModifiers(target, armor);

    // Apply Armor to Damage
    if (armor >= 0) {
        value *= 100 / (100 + armor);
    } else {
        value *= 2 - (100 / (100 - armor));
    }
}

// Return Value
return (isNaN(value) ? 0 : value) * sign;

I hope for some one can help me out.

Kind regard
Arthoras
is your base power supposed or be a static number, or is it actively calculated?

im not familiar with what those VS plugins let you do, but if that JS is being ran somewhere and a window is able to access it to display a value, you need a "b" variable. unless your damage formulas dont factor in the target's parameters, its not going to be able to calculate a damage value with just an attacker.

Edit: also an "id" variable
Your formatting is making things difficult to read. You should use the "Code" button in the post editor. You can also wrap your JavaScript in the appropriate BB code.

Hard to say much else without seeing properly-formatted code and the formulas you are trying to parse. You should be using console.log() to output some debug statements for yourself, such as whether you're correctly fetching the actor, the skill id, the actual skill object, etc.

Two other things to note:
  1. Maybe consider making sure you have a valid actor before trying to parse/evaluate a formula. Do the same for the skill, etc. Don't assume you are retrieving valid data.
  2. If you are using eval() in this context, I'm pretty sure you will trigger any additional stuff in the damage formula, such as adding states via a.addState(x), so use caution or adjust your method if you plan to use the damage formula box in that manner.
It's not impossible to do, but there are a few things you'd need to be aware of.

Does your damage formulas incorporate the stats of the recipient of the skill?
(For example, b.def)
If so, you'd have to find away to create a version of the skill that does not.
If it were there, it would likely crash the game when it tried to get that info.
(This may not matter if this is being done in battle and not the menu, tho that would complicate things a bit as well.)

Next, it would be a good idea to just make the value equal to a variable, so you can just use the \V[] shortcut to provide the number.

Outside of those two things, it would probably be fairly easy to do.

EDIT: I made a quick plugin to help, but since I felt like being acknowledged here and wasn't, I'll just drop in my pastebin later. 🙂
is your base power supposed or be a static number, or is it actively calculated?

im not familiar with what those VS plugins let you do, but if that JS is being ran somewhere and a window is able to access it to display a value, you need a "b" variable. unless your damage formulas dont factor in the target's parameters, its not going to be able to calculate a damage value with just an attacker.

Edit: also an "id" variable
the number should be actively calculated . It only uses an attacker in the formula. the damage to the target is calculated through the skill core plugin.
One of the problems is that i don't know how to retrieve the id of a selected skill in a menu window.

Your formatting is making things difficult to read. You should use the "Code" button in the post editor. You can also wrap your JavaScript in the appropriate BB code.

Hard to say much else without seeing properly-formatted code and the formulas you are trying to parse. You should be using console.log() to output some debug statements for yourself, such as whether you're correctly fetching the actor, the skill id, the actual skill object, etc.

Two other things to note:
  1. Maybe consider making sure you have a valid actor before trying to parse/evaluate a formula. Do the same for the skill, etc. Don't assume you are retrieving valid data.
  2. If you are using eval() in this context, I'm pretty sure you will trigger any additional stuff in the damage formula, such as adding states via a.addState(x), so use caution or adjust your method if you plan to use the damage formula box in that manner.
i did the formatting of the code. will add console.log() to the js.

I made some progress. I'm currently stuck on retrieving the id of the selected skill.
Here is the current code
JavaScript:
// Check if in battle or not
if (SceneManager._scene instanceof Scene_Battle)
{    // Get actor id when in battle
    a = BattleManager.actor();
}
// For the other menus
else
{    // Get actor id in the menu
    a = $gameParty.menuActor();
}
// For testing, uccessfuly returns hp of selected actor in the skill description in battle and in menu
return a.hp
while in battle, you can use
Code:
SceneManager._scene._skillWindow.item()
to access the current skill. so from there you can use item().id and item().damage.formula to get the id and formula.

cant help you with the skiill shop as thats added by VS and is going to be obfuscated. itll likely be a similar line to above though. while skill shop is open, try using the console to look at the properties of SceneManager._scene and see if you can figure out the window that handles the skill selection
while in battle, you can use
Code:
SceneManager._scene._skillWindow.item()
to access the current skill. so from there you can use item().id and item().damage.formula to get the id and formula.

cant help you with the skiill shop as thats added by VS and is going to be obfuscated. itll likely be a similar line to above though. while skill shop is open, try using the console to look at the properties of SceneManager._scene and see if you can figure out the window that handles the skill selection
Thanks for the info. i tried it but i cant get it to work. I don't know how or where to use item().id and item().damage.formula in the js code.

If the skill shop cant be done that's ok, but how do i do it in the normal skill menu?

Here is t the current code:

JavaScript:
// Check if in battle or not
if (SceneManager._scene instanceof Scene_Battle)
{    // Get actor id when in battle
    a = BattleManager.actor();
    item = SceneManager._scene._skillWindow.item()
}// Check if in skill shop menu
else
{    // Get actor id in the menu
    a = $gameParty.menuActor();
}
// Display "power" of skill
const skill_dmg = item.damage.formula;
return skill_dmg

All that does is to display the formula of the skill in the description and not a number
All that does is to display the formula of the skill in the description and not a number
you need to run the formula through an eval() like you did earlier
let value = Math.max(eval(formula), 0); //<- Copied from the damage formula "Armor Scaling" from the battle core
otherwise, you only have the text for the formula
you need to run the formula through an eval() like you did earlier

otherwise, you only have the text for the formula
thank you very much, that works in battle with most of the skills. the ones that don't work is the default attack command and skills that replaces it.
Is there a way to retrieve the id of the skill that is the current attack command or would i need to do it manually ant make a text macro for each of those skills?

Do you know how to retrieve the id of the selected skill in the main menu?

Where do you find info like this "SceneManager._scene._skillWindow.item()". Is there a document or a wiki where those are listed?
Is there a way to retrieve the id of the skill that is the current attack command
you already use "a" as the reference to the actor, so its just a.attackSkillId()

Where do you find info like this "SceneManager._scene._skillWindow.item()". Is there a document or a wiki where those are listed?
most of MV and MZ are the same, so this online documentation as long as toure not dealing with windows and a few other specific areas is accurate

but ultimately the code is in the js folder of your project. The files are organized in a way that if you know what youre looking for, you can tell which file has it and see how and where its used

this is assuming you know enough JS to read the code and understand what it does
you already use "a" as the reference to the actor, so its just a.attackSkillId()


most of MV and MZ are the same, so this online documentation as long as toure not dealing with windows and a few other specific areas is accurate

but ultimately the code is in the js folder of your project. The files are organized in a way that if you know what youre looking for, you can tell which file has it and see how and where its used

this is assuming you know enough JS to read the code and understand what it does
thank you for the help. it works now in the battle. the only thing left is to get it to work in the skill menu (ESC -> Skill -> select character -> select skill type -> select skill) to display the number.

Dos some on know how to retrieve the skill id of a selected skill in the menu?
Dos some on know how to retrieve the skill id of a selected skill in the menu?
its the same code, as i have given before, but its ._itemWIndow instead of ._skillWindow

Edit: never noticed the itemBase scene's item function. you dont need to know what the window is called. try
Code:
SceneManager._scene.item()
for all of your scenes except the battle scene. the VS one might implement it too
its the same code, as i have given before, but its ._itemWIndow instead of ._skillWindow

Edit: never noticed the itemBase scene's item function. you dont need to know what the window is called. try
Code:
SceneManager._scene.item()
for all of your scenes except the battle scene. the VS one might implement it too
thanks for the help.
Code:
SceneManager._scene.item()
works for the menu. but dosen't work for the VisuMZ_4_SkillShop plugin.

I updated my fist post with the current solution of JS code that i use, which works so far.

As far as i'm concerned. the problem is solved and the tread can be closed.

Original thread: https://forums.rpgmakerweb.com/threads/damage-formula-in-skill-description.175215/