Trying to change counterattack; need a little help, please? (YEP Battle Engine Core)

I'm trying to set up counterattack so that when the player uses ranged attacks, the enemy can't counterattack. I have three ranged type weapons that should not provoke counters. So far, I've been able to make it so that enemies won't counterattack these weapon types, but when the enemy attacks, the program is trying to check if the enemy has N weapon equipped and crashes due to a null error, being that enemies don't have equipped weapons to check for.
I'm using YEP_BattleEngineCore; this is on line 2094
Spoiler: My code so far
else if ( !(BattleManager.actor().isWtypeEquipped(20)) && !(BattleManager.actor().isWtypeEquipped(19)) && !(BattleManager.actor().isWtypeEquipped(18)) && (Math.random() < this._action.itemCnt(target))) {
Do a check to see if the attacker is an actor or enemy.
Do a check to see if the attacker is an actor or enemy.


I tried that with adding (!(user.isEnemy()) && ... ) and I think what it's doing is validating that user is not enemy, but continues to check the other conditions; I also tried to write it into a two-part if/then to say if (user.isEnemy()) { do regular function } else if !(user.isEnemy()) { do big crazy function } but it just didn't seem to work.

I'm amateur as can be with js, so no doubt it's user error. I'll probably keep trying that split if/else thing until maybe it eventually works...

EDIT: What I think would be easiest is if I could somehow have it validate a null return? If that's even possible? But then, I suppose that would be like an extra if/else to say if value == null, do something

I've moved this thread to Plug-In support. Thank you.

I tried that with adding (!(user.isEnemy()) && ... ) and I think what it's doing is validating that user is not enemy, but continues to check the other conditions; I also tried to write it into a two-part if/then to say if (user.isEnemy()) { do regular function } else if !(user.isEnemy()) { do big crazy function } but it just didn't seem to work.

I'm amateur as can be with js, so no doubt it's user error. I'll probably keep trying that split if/else thing until maybe it eventually works...

EDIT: What I think would be easiest is if I could somehow have it validate a null return? If that's even possible? But then, I suppose that would be like an extra if/else to say if value == null, do something

Ya your plan is what I would do as well. What does the documentation say about the expected formula? Does it check if the formula resolves to a boolean value?
Sooo, I got it working, I guess? I split it into two separate statements within the statement; gave an enemy 100% counter, he countered non-ranged weapons, did not counter ranged weapons. And regular attacks from the actor or enemy did not cause crashes.

Using Yanfly's Battle Engine, I used subject.isEnemy and subject.isActor rather than user.is...

For posterity, this is what the resulting code looks like (again, using YEP_BattleEngineCore, although RTP js should handle it fairly similarly):

Spoiler: CODE (note weapon TYPES 18, 19, and 20 are my ranged ones)
BattleManager.invokeAction = function(subject, target) {
if (!Yanfly.Param.BECOptSpeed) this._logWindow.push('pushBaseLine');
var normal = true;
if (Math.random() < this._action.itemMrf(target)) {
this.invokeMagicReflection(subject, target);
} else if ((subject.isEnemy()) && ((Math.random()) < this._action.itemCnt(target))) {
this.invokeCounterAttack(subject, target);
} else if (Math.random() < this._action.itemMrf(target)) {
this.invokeMagicReflection(subject, target);
} else if ((subject.isActor()) && (!(BattleManager.actor().isWtypeEquipped(20))) && (!(BattleManager.actor().isWtypeEquipped(19))) && (!(BattleManager.actor().isWtypeEquipped(18))) && (Math.random() < this._action.itemCnt(target))) {
this.invokeCounterAttack(subject, target);
} else {
this.invokeNormalAction(subject, target);
}
if (subject) subject.setLastTarget(target);
if (!Yanfly.Param.BECOptSpeed) this._logWindow.push('popBaseLine');
};


But thank you very much for taking the time to read and make suggestions. (Will be contacting you, soonish, Hime as my project uses several of your scripts! 😉 - I just want something a little more substantial before I start showing it off).

Going to report this thread for solved. Still have a nagging feeling something will come back to bite me for this.

EDIT: Double checked magic and magic reflection as well as player counterattack and everything is going perfectly... too perfectly. BUT, I wanted to note for people who might find this useful, this is not designed to determine if enemies are using ranged weapons! Just the player. If you want your enemies to have attacks that ignore player counterattack, the solution SHOULD be to just set the attack to certain hit. At least it will be for me. I hope this helps some people down the road.

Thanks again, folks!
Oh I didn't realize you're editing the plugin directly. I thought the plugin would provide a way for you to customize the counterattack behavior through like a formula or something.

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.

Original thread: https://forums.rpgmakerweb.com/threads/trying-to-change-counterattack-need-a-little-help-please-yep-battle-engine-core.124850/