Issue with Eval() returning an exception

Status
Not open for further replies.

RedFoxGaming

Veteran
Veteran
Joined
Jan 14, 2018
Messages
88
Reaction score
16
First Language
English
Primarily Uses
RMMV
So I am currently working on my first plugin and I am brand new to JS. I'll post the code in a spoiler and then I'll explain the issue.
Code:
/////////////////////////////////////////////////////////////////
// RFG-Multiple Hits System
// Author: RedFoxGaming
/////////////////////////////////////////////////////////////////
/*:
 * @plugindesc A Multiple Hits attack system similar to Final Fantasy
 * @author RedFoxGaming
 * 
 * This plugin has no plugin commands
 *
 * @param Maximum Possible Hits
 * @desc The maximum possible amount of hits, without state modifiers
 * @default 16
 * 
 * @param Maximum Magic Hits
 * @desc The maximum possible magic multipliers
 * @default 6
 * 
 * @param Double Hits State
 * @desc The stateID of the state you want to double your hits (will go beyond max)
 * @default 10
 * 
 * @param Half Hits State
 * @desc The stateID of the state you want to half your hits
 * @default 11
 * 
 * @param Possible Hits Formula
 * @type text
 * @desc The formula that calculates how many hits are possible
 * @default agi / 16
 * 
 * @param Damage Formula
 * @type text
 * @desc The default damage formula
 * @default atk * 4 - def * 2
 * 
 * @param Magic Damage Formula
 * @desc The default damage formula for spells
 * @default base + a.mat * 2 - b.mdf * 2
 * 
 * @param Actual Hits Variable
 * @desc The ingame variable to store your hits scored (to use in messages)
 * @default 1
 * 
 * @help
 * 
 * ----------------------------------------------
 * RFG Multiple Hits System
 * Version 0.1
 * ----------------------------------------------
 * 
 * The purpose of this plugin is to mimic the way Final Fantasy handles their
 * attacking system, where your attack has the possibility of landing multiple
 * hits. In order to use this plugin, all you have to do is replace your formulas
 * in the skills you have set to attack, or your magic formulas to my functions.
 * 
 * Function for Physical Attacks
 * this.physicalAttack(a,b)
 * 
 * Function for Magical Attacks
 * this.magicalAttack(a,b,base) where base is a default number value
 * 
 * Function for Healing Magic
 * this.healingMagic(a,b,base) where base is a default number value
*/
function getRandomInt (min,max) { //Define a function to get a random range of numbers
    min = Math.ceil(min);
    max = Math.floor(max) + 1;
    return Math.floor(Math.random() * (max - min)) + min;
}
    function toNumber(str, def) {
        return isNaN(str) ? def : +(str || def);
    }
    var parameters = PluginManager.parameters("RFG-MultipleHitsSystem");
    var mhitsposs = toNumber(parameters['Maximum Possible Hits'], 16);
    var mmagicposs = toNumber(parameters['Maximum Magic Hits'], 6);
    var doublehitsstate = toNumber(parameters['Double Hits State'], 10);
    var halfhitsstate = toNumber(parameters['Half Hits State'], 11);
    var phitsform = parameters['Possible Hits Formula'] || "a.agi / 16";
    var damageform = parameters['Damage Formula'] || "a.atk * 4 - b.def * 2";
    var magicdamform = parameters['Magic Damage Formula'] || "base + a.mat * 2 - b.mdf * 2";
    var actualhitsvar = toNumber(parameters['Actual Hits Variable'], 1);
    var phits = 0;
    var damage = 0;
    Game_Action.prototype.physicalAttack = function(a,b){
        $gameVariables.setValue(actualhitsvar, 0);
        phits = eval(phitsform);
        if(phits < 1) { phits = 1; }
        if(a.isStateAffected(doublehitsstate)) { phits = phits * 2; }
        if(a.isStateAffected(halfhitsstate)) { phits = phits / 2; } 
        for(i=0; i < phits; i++){
            if(Math.random() > 1 - a.acc && Math.random() > b.eva) { //If both of these conditions are true, than this was a successful hit
                damage = damage + eval(damageform);
                $gameVariables.setValue(actualhitsvar, $gameVariables.getValue(actualhitsvar) + 1); //Add one to actual hits if sucessful
            }
        }
        console.log("Damage: " + damage + " | Possible Hits: " + phits + " | Actual Hits: " + $gameVariables.getValue(actualhitsvar));
        if(damage > 0) {
            if(Math.random() < a.cri){
                damage = damage * 2;
                if(a.isActor() == true) { SceneManager._scene._logWindow.push(TextManager.criticalToEnemy); }
                if(a.isActor() == false) { SceneManager._scene._logWindow.push(TextManager.criticalToActor); }
                return damage;
            }
            else { return damage; }
        }
        else{
            if(a.isActor() != true){
                SceneManager._scene._logWindow.push(TextManager.enemyNoHit);
                return 0;
            }
            else{
                SceneManager._scene._logWindow.push(TextManager.actorNoHit);
            }
        }
    }
    Game_Action.prototype.magicalAttack = function(a,b,base){
    }
    Game_Action.prototype.healingMagic = function(a,b,base){
    }

This code is not finished yet, but what I am trying to do here is make a multiple hit calculation similar to the Final Fantasy games. The issue is the damage returned is always 0. I had to add breakpoints and step through each individual line to figure out what's going on, and I'm still not sure why. The script proceeds as normal until it gets to phits = eval(phitsform); at this point, in the game's built in damage calculation function, an exception is getting caught right here, and in the game's built in Try Catch block, it is catch(e) { return 0; }, and this is why I'm getting 0 damage.

I don't know why the formula is failing however. And because of this, I cannot see if the rest of the plugin is actually functioning properly. I'm sure it's something simple like a typo I'm not seeing.
 

Shaz

Veteran
Veteran
Joined
Mar 2, 2012
Messages
38,492
Reaction score
12,035
First Language
English
Primarily Uses
RMMV
Just a guess, but should this:

Code:
* @param Possible Hits Formula
* @type text
* @desc The formula that calculates how many hits are possible
* @default agi / 16
*
* @param Damage Formula
* @type text
* @desc The default damage formula
* @default atk * 4 - def * 2
be this?
Code:
* @param Possible Hits Formula
* @type text
* @desc The formula that calculates how many hits are possible
* @default a.agi / 16
*
* @param Damage Formula
* @type text
* @desc The default damage formula
* @default a.atk * 4 - b.def * 2
 

RedFoxGaming

Veteran
Veteran
Joined
Jan 14, 2018
Messages
88
Reaction score
16
First Language
English
Primarily Uses
RMMV
Oh I didn't even see that. Yeah, I suppose Agility only exist within objects so I take it the engine has no idea what agi is. Yeah that's suppose to be a.agi.

Edit: Oh I typoed the damage formula too. Even better :/

Edit 2: Yeah, I got the issue fixed now. This thread is solved. I'll make a separate thread for the other issue I am having since it is not related to eval(). This thread can be closed. Thanks!
 
Last edited:

Shaz

Veteran
Veteran
Joined
Mar 2, 2012
Messages
38,492
Reaction score
12,035
First Language
English
Primarily Uses
RMMV

Kes

Global Moderators
Global Mod
Joined
Aug 3, 2012
Messages
21,614
Reaction score
10,925
First Language
English
Primarily Uses
RMVXA

This thread is being closed, due to being solved. If for some reason you would like this thread re-opened, please report this post and leave a message why. Thank you.

 
Status
Not open for further replies.

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Latest Threads

Latest Profile Posts

Kingdom Under Fire: The Crusaders is finally being ported to PC. I loved playing that game on the original Xbox.
A friend told me to stop making nomnom or gulping noise when eating or drinking. Plot twist of my life. I thought people can't hear that!
Inside you are two wolves. They take in oxygen while getting rid of the Co2 gas in your body...
Ah wait, sorry. Lungs. Not wolves. that'd be silly. Wolves are huge. You probably couldnt even fit one wolf inside you.
Tag: hack and slash | gameplay: point and click. Me: "How tf two are related to each other?"

Forum statistics

Threads
94,407
Messages
920,731
Members
124,196
Latest member
DysonY
Top