MV Force An Action To Miss? Dynamic Accuracy/Evasion?

Hey everyone, looking for support and/or advice.

I want to incorporate a mechanic where flying enemies are hard to hit unless you use a ranged weapon/skill. I've been experimenting and racking my brain trying to develop the best ways to implement this.

My latest attempt was to use Yanfly's Buffs and States Core and Auto Passive States. I made a "Flying" passive state and gave it the following note tags to check if the incoming attack is ranged, and if not, to enforce a 50/50 chance of a forced miss:
JavaScript:
<Custom Select Effect>
// Check if the action deals HP damage
if (this.isHpEffect()) {
  //ID of Ranged element
  var rangedElement = 6;
  // Get the current elements of the action
  var elements = this.getItemElements();
  //If the incoming attack is not ranged
  if (!elements.contains(rangedElement)) {
    //50% chance of a forced miss, roll for it
    var coinFlip = Math.random();
    if (coinFlip < 0.5) {
      //Lost the coin toss, it's a miss
      b.result().missed = true;
    }
  }
}
</Custom Select Effect>
Unfortunately, that doesn't seem to do it. Whether the coin toss comes up one way or the other, the hit/HP damage executes as normal.

I tried using the <Custom React Effect> tag from the Buffs and States Core as well, but all that did was make the "Miss" popup occur twice; the damage and hit sound were still applied as normal. I went with <Custom Select Effect> because the instructions said it would execute after the target is selected, but before damage calculation.

While I'm comfortable with JavaScript, I'm still not totally well-versed in how RPG Maker MV works under the hood. Delving into rpg_objects.js a bit, I suspect the problem may lie in how hit/damage calculation is handled, but I thought I'd ask if someone more experienced has any advice.

Is there a way to fix my notetags to get the desired result? If this isn't a viable strategy, is there a reasonable alternative to implement this mechanic? I'm not attached to the coin toss solution. If there's a way to say fluxuate accuracy or evasion based on the state applied and element used against the enemy, I'm open-minded to a solution like that.

Any solution that wouldn't be horrendously tedious to implement across every flying enemy in the game would be taken into consideration and much appreciated.

Thank you for your time. 🙂
While I'm comfortable with JavaScript, I'm still not totally well-versed in how RPG Maker MV works under the hood. Delving into rpg_objects.js a bit, I suspect the problem may lie in how hit/damage calculation is handled, but I thought I'd ask if someone more experienced has any advice.
its because of when all of the code in those notetags gets executed. within Game_Action.apply(), the first thing that happens is that the result gets cleared and then damage execution is called within it after the hit check. so any manipulation of the result before apply() gets overwritten (select and initiate), and any manipulation after the hit check is already too late (literally all the other notetags)

Depending on how comfortable you are with JS and MV, you could make your own small patch to Game_Action.itemHit() to factor in the flight state and the action's range (its easier than it sounds), use yanfly's hit accuracy plugin to essentially do the same, or use select and deselect effects to apply and remove a state with a 50% accuracy reduction to the attacker or a +50% eva to the defender so that the action is likelier to miss

theres also an approach like this one where you'd use select and deselect effects to push and remove an evasion trait into the flight state, but i havent tried it out myself to see if theres a condition that'd cause the trait to be applied, but not be removed
its because of when all of the code in those notetags gets executed. within Game_Action.apply(), the first thing that happens is that the result gets cleared and then damage execution is called within it after the hit check. so any manipulation of the result before apply() gets overwritten (select and initiate), and any manipulation after the hit check is already too late (literally all the other notetags)
I suspected it might be a problem like that, thank you for the clarity.
Depending on how comfortable you are with JS and MV, you could make your own small patch to Game_Action.itemHit() to factor in the flight state and the action's range (its easier than it sounds)
I might try this approach, I'm a little skittish about patching base code just because I don't want to break compatability with other plugins I might use, but it seems like a simple and unintrusive tweak.
theres also an approach like this one where you'd use select and deselect effects to push and remove an evasion trait into the flight state, but i havent tried it out myself to see if theres a condition that'd cause the trait to be applied, but not be removed
I suppose the Flying state applying and removing a Dodge state is a pretty straightforward approach, dunno why I didn't think of it.

I didn't implement it in the exact same way as this thread but did a simple test with these notetags and it seems to be giving the desired result:
JavaScript:
<Custom Select Effect>
// Check if the action deals HP damage
if (this.isHpEffect()) {
  //ID of Ranged element
  var rangedElement = 6;
  // Get the current elements of the action
  var elements = this.getItemElements();
  //If the incoming attack is not ranged
  if (!elements.contains(rangedElement)) {
    //Apply Dodge state before hit calculations
    b.addState(13);
  }
}
</Custom Select Effect>
<Custom Deselect Effect>
//Remove Dodge state after hit calculations
b.removeState(13);
</Custom Deselect Effect>
Like you said in your reply though, I haven't thoroughly tested it yet to see if there's a case where the state is applied but not removed.

Thank you for your help. I'd like to refrain from closing this thread as solved for the time being though, giving me time to run a few more tests and possibly hear other suggestions.

Original thread: https://forums.rpgmakerweb.com/threads/force-an-action-to-miss-dynamic-accuracy-evasion.177716/