MV Unique common complex damage formula

Hello everyone! I'd like to have 1 single complex damage formula for most of the skills of my game. What is the smartest way to achieve it? Can I just call from within the damage formula field a function
and I then define the function on a separate js file as a plugin?
Thank you

(let's say for example:
skill 003: Fire
damage formula: let damage

then in damage.js
let damage = {
(a.atk+log(a.atk*2/b.def)-10/b.def)/2*.... etc. etc....}
There is no such term as universal damage formula, unless your game plot explains it rationally, and yes, you can use function from plugin as damage, but you must remember important thing
that function MUST ALWAYS RETURN NUMERIC RESULT:

example:

Code:
wrongFormula = function(a, b){
$gameParty.gainGold(1000);
}

Skill with this formula may give 1000 gold to team, but after that either it will deal 0 damage, or crash game, proper formula looks like this:

Code:
correctFurmula = function(a, b){
return ((a.atk * 2) + (a.mat * 3) - b.def) || 1;
}

then in skill formula field in database you just put correctFormula(a,b);

explanation:
return x || y

means if x is properly calculated (with no errors, syntax error) it will deal calculated damage, otherwise it will deal y damage value (I recommend using a fixed value, using comma values (1.1 ex) causes hp/mp bars to sligthly glitch, so the enemy might have hp: 2000.89 - yes, it's calculated properly, but looks ugly.)

and another pro-tip, before return value in skill function you can use any script call available in MV and that code will execute, so you can do for example this:

Code:
correctFurmula = function(a, b){
$gameParty.gainGold(1000);
$gameActors.actor(1).recoverAll();
return ((a.atk * 2) + (a.mat * 3) - b.def) || 1;
}

this mean, your skill with this will give 1000 gold, recover HP and MP for actor #1 from database and deal damage according to your formula.

You can also reffer to current ability user, by using a variable from function argument:

Code:
correctFurmula = function(a, b){
$gameParty.gainGold(1000);
a.recoverAll();
return ((a.atk * 2) + (a.mat * 3) - b.def) || 1;
}

and so on - limitation is only your imagination.

Edit 4:
I never checked how much you can nest return values in function such as:
Code:
function test(){
return 1 || 2 || 3 || 4 || 5; //be default it should show 1 in console.
}

Hope I get some time to check it later.
Here's working and secured plugin you can use to play/learn/use:
JavaScript:
(function(){

    _skillFormula = function(a,b){
      
        if (SceneManager._scene instanceof Scene_Battle){
          
            //setup damage formula
          
            let formula = (a.mat * 2) - b.def; //edit this line to define your formula. Nothing else.
          
            return formula || 1;
        }
      
        // If player is outside the battle, let's force skill to
        //return 0 damage since battles are occuring only
        //in Scene_Battle scene. It'a a security purpose!
      
        else return 0;
    }

})();

and use it in skills database like in this image:
Here's working and secured plugin you can use to play/learn/use:
if you encapsulate the function _skillFormula in IIFE, can you actually use it in the damage formula though?
Yes @TheoAllen - I've just checked in my MV 1.6.2 and it's working properly.
Want to see a video?
oh thank you for the excellent explanation! Yes if you can share a video it would be supreme!! 😀
You're welcome 🙂

thank you very much, you're great!
Can I ask you even another thing, you're so kind:
if I'd like to add a variable as a parameter to be defined within the formula, I change like this:


formula = ((a.mat * 2) - b.def) * var1 * var2

and previously I declare the var value such as (var1 being flat value, while var2 checking a switch before deciding which value):

let var1 = 2

let var2 =
if ($gameSwitches.value[1] == true) {
return 4;
}
else return 1;


So in the end something like this :

Code:
let var1 = 2;

let var2 =
if ($gameSwitches.value[1] == true) {
         return 4;
       }  
        else return 1;

(function(){

    _skillFormula = function(a,b){
     
        if (SceneManager._scene instanceof Scene_Battle){
         
            //setup damage formula
         
          formula = ((a.mat * 2) - b.def) * var1 * var2
         
            return formula || 1;
        }
     
        // If player is outside the battle, let's force skill to
        //return 0 damage since battles are occuring only
        //in Scene_Battle scene. It'a a security purpose!
     
        else return 0;
    }

})();

Did I made mistakes? Thank you very much!
Yes, there are errors in your code:
first of, all conditional instructions must be inside function not outside.
second if you want to declare a variable inside a function, then you must use keyword var variable_name = value or let variable_name = value;
third thing, indexes of switches and variables ($gameSwitches, $gameVariables) are using round brackets.

Here is explanation of difference between var and let I explained few days ago.

Your code should look like this:

JavaScript:
(function(){

    _skillFormula = function(a,b){
     
        if (SceneManager._scene instanceof Scene_Battle){
            
            let var1 = 2;
            let var2 = 0; //How much? it was empty!
            
            //You can use $gameVariables.value(x) to use variables from game.
         
            //setup damage formula
         
            let formula = ((a.mat * 2) - b.def) * var1 * var2;
         
            return formula || 1;
        }
     
        // If player is outside the battle, let's force skill to
        //return 0 damage since battles are occuring only
        //in Scene_Battle scene. It'a a security purpose!
     
        else return 0;
    }

})();

I don't know what you need this piece for...
Code:
    if ($gameSwitches.value(1) === true)
    {
        return 4;
    }  
    else return 1;
thank you!

This code was to say: if switch nr1 is true the var2 = 4, if it is false, then var2 = 1


if ($gameSwitches.value(1) === true)
{
return 4;
}
else return 1;
No no no.

in function block keyword return means basicly "give me this!" and in point where keyword return is typed function ends. So it should look like this:

JavaScript:
(function(){
    _skillFormula = function(a,b){
        
        if (SceneManager._scene instanceof Scene_Battle){
            var var1 = 0;
            var var2 = 1; 
            
            //If you set it as zero and I see it's included
            //in formula equasion then I bet you know what you get
            //if you multiply any number by zero - you get zero.

            if ($gameSwitches.value(1) === true)
            {
                    var1 = 4; 
            }
            else var1 = 1;
         

            //setup damage formula
            let formula = ((a.mat * 2) - b.def) * var1 * var2;
            
            return formula || 1;
        }
        else return 0;
    }
})();
Appreciate your help! It works now
@joket - is the problem solved? If so, I'll close the thread. 👋


@Soulrender - just so you know, the IIFE in your code serves no useful purpose because _skillFormula is declared implicitly, which makes it a global function. That's not a practical problem here because you want the function to be global, but it is a confusing way to write things. 😅

A non-implicit declaration would use var, let, or const, or directly reference a parent object, e.g. window._skillFormula. More details:
Stuff like this is one reason I prefer strict mode~
Caethryll so how would you mod the code?

p. s: problem is solved, but if you can leave the thread open, we could avoid double posting if new doubt or questions arise on the same argument 🙂
OK, I'll leave it open for now~

For information:
  • On these boards, "double posting" means posting twice in a row without waiting 72 hours between posts. The rule is mostly to minimise spam.

  • By contrast, support threads are generally closed to prevent stuff like hijacking ("I have the same problem" "no you don't") or topic drift: things that can make it difficult for future people to find relevant answers in your thread.

Caethryll so how would you mod the code?
As mentioned, Soulrender's code looks functionally OK as it is. The changes I'm about to suggest are mainly for readability.

Personally, I would add this new line just after (function(){:
  • 'use strict';
...and explicitly declare _skillFormula as a member of an object, e.g.
  • window._skillFormula = function(a, b) {
The === true in the switch check can technically be removed, since $gameSwitches.value(1) should be a Boolean value, i.e. true or false. Also, for consistency I would swap the var declarations for let, like they were in post #8.

Original thread: https://forums.rpgmakerweb.com/threads/unique-common-complex-damage-formula.142977/