MZ Hit, Evasion and Game_Action.prototype.apply = function(target)

Hit, Evasion and Game_Action.prototype.apply = function(target) ​

I have been attempting to overwrite the functionality of this almost completely.

Issue Summary — I'm attempting to make Hit, Luk, and Eva impact physical attacks accuracy and evasion rates - I'm also trying to make Mdf, Mat, Luk, and Mev impact magic evasion. I found the functions that impact it but for some reason the declared constants aren't working as intended.

Details — When I plug the script in the game loads but crashes on attack stating "diffroll1" hasn't been defined. I have it set with an "if" "else" statement within the code however it says it isn't defined...

What tutorials or previous support threads have you referenced? — I've looked at support for Visustella's battle core and tried the ATT_Turan hit formula script ( it crashes when loaded every time. )

Are you using any plugins? — Yes, I'm using Visustella's Battle Core, Status Core, Menu Core, ItemsEquipCore, SkillsStatesCore, SkillLearn, and SRD_WalkCharacterBattlers.

Attach any illustrative screenshots, full event pages with no cropping, mock ups, etc. —
1754795164950.png



This is the script code:
Spoiler: Script
/*:
* @target MZ
* @plugindesc Hit and Evasion mods by Clarx
* @Author Clarx
*
* @Help
* This makes the hit formula and evasion formula stat based using AGI and LUK for physical, MAT MDF and LUK for magic.
*
*/

const CLARX_HitEva = {};


// Hit calculations



Game_Action.prototype.itemHit = function(/*target*/) {
const successRate = this.item().successRate;
if (this.isPhysical()) {
let rough = successRate + this.subject().hit + ( this.subject().agi / 5 ) + ( this.subject().luk / 10 );
let rounded = Math.trunc(rough);
return rounded;
} else {
let roughm = successRate + ( this.subject().mat / 5 ) + ( this.subject().mdf / 5 ) + ( this.subject().luk / 10 );
let roundedm = Math.trunc(roughm);
return roundedm;
}
};


// Eva calculations

Game_Action.prototype.itemEva = function(target) {
if (this.isPhysical()) {
return target.eva + ( target.agi / 2 ) + ( target.luk / 10 );
} else if (this.isMagical()) {
return target.mev + ( target.mdf / 3 ) + ( target.mat / 5 ) + ( target.luk / 10 ) ;
} else {
return 0;
}
};

Game_Action.prototype.itemCri = function(target) {
return this.item().damage.critical
? this.subject().cri * (1 - target.cev)
: 0;
};

Game_Action.prototype.apply = function(target) {

// New variables



// End new variables


const result = target.result();
this.subject().clearResult();
result.clear();
result.used = this.testApply(target);
result.physical = this.isPhysical();
if (this.isPhysical()) {
const diffroll1 = ( target.agi / 2 ) + ( target.luk / 10 ) + target.eva;
} else {
const diffroll1 = ( target.mdf / 4 ) + ( target.mat / 4 ) + ( target.luk / 10 ) + target.mev;
}
const diffroll = Math.trunc(diffroll1);

// This is where misses and evasions actually happen. > greater, < less

result.missed = result.used && Math.randomInt(diffroll) >= this.itemHit(target);
result.evaded = !result.missed && Math.randomInt(diffroll) < this.itemEva(target);

// Nothing below is modified.


result.drain = this.isDrain();
if (result.isHit()) {
if (this.item().damage.type > 0) {
result.critical = Math.random() < this.itemCri(target);
const value = this.makeDamageValue(target, result.critical);
this.executeDamage(target, value);
}
for (const effect of this.item().effects) {
this.applyItemEffect(target, effect);
}
this.applyItemUserEffect(target);
}
this.updateLastTarget(target);
};

I'm attaching the script below as well for ease of use.
When I plug the script in the game loads but crashes on attack stating "diffroll1" hasn't been defined. I have it set with an "if" "else" statement within the code however it says it isn't defined...
diffroll1 is a const that youve defined inside an IF. consts dont exist outside of the scope theyre declared in and diffroll1 would be undefined where youre using it. use var or define it as a let before the IFs

also...

I've moved this thread to Javascript/Plugin Support. Thank you.

There are ways to approach this (here are a couple of ways):

- use var/let before the if scope so you can assign a value to the variable, or

- assign it directly to the variable when declaring it (including math trunc to only use one variable instead of two):
JavaScript:
// declare variable outside of scope for later assignment
let diffroll1;
if (result.physical) { // use result.physical since it is already declared
  diffroll1 = ( target.agi / 2 ) + ( target.luk / 10 ) + target.eva;
} else {
  diffroll1 = ( target.mdf / 4 ) + ( target.mat / 4 ) + ( target.luk / 10 ) + target.mev;
}
const diffroll = Math.trunc(diffroll1);
JavaScript:
const diffroll = Math.trunc(result.physical
  ? (target.agi / 2) + (target.luk / 10) + target.cev
  : (target.mdf / 4) + (target.mat / 4) + (target.luk / 10) + target.mev
);

I am not sure this solves everything since there is still more code to run so hope it gets you to the next step.
So that solved it, but now Visustella's Skills and States core is throwing an error stating user.loseAbilityPoints isn't defined? never had an issue before
tried the ATT_Turan hit formula script ( it crashes when loaded every time. )
It looks like you may have resolved some degree of your situation, but it would always help if you reported a crash including a screenshot of the Console tab with the error message and trace that you see when you press F8.

Many other people use it, have reported using it with VisuStella products, and I just double checked with my MZ 1.9 test project - it does not crash when the game loads.

The plugin should be able to fairly easily achieve what you wanted to do without you having to come up with nearly this much code - if you need any help getting it to work, please post in the plugin thread with that information.

Original thread: https://forums.rpgmakerweb.com/threads/hit-evasion-and-game_action-prototype-apply-function-target.179483/