Script that redirect attack on an ennemy with specific state

Hi, I have created a Taunt state which force the ennemies to focus their physical attacks on your ally with the taunt state.

My problem is when the spell is casted during the turn, nexts ennemies attacks will not be redirect to my ally who use Taunt.

I'm using Yanfly's target core to create a custom target eval on my basic physical attack :

JavaScript:
<Custom Target Eval>
  var members = foes.aliveMembers();
  var tauntTargets[];
  var clearInitialTargetDone = false;
  targets.push(target);

  for (var i = 0; i < members.length; ++i)
  {
      if(members[i].isStateAffected(5))
      {
          tauntTargets.push(members[i]);

          if(!clearInitialTargetDone)
          {
                 targets.pop();
                 clearInitialTargetDone = true;
          }
      }
  }

  if(tauntTargets.length > 0)
  {
      targets.push(tauntTargets[Math.randomInt(tauntTargets.length -1)]);
  }

</Custom Target Eval>

NB : if multiple allies or ennemies have the taunt state, we choose randomly the target of the attack (with the taunt state, obviously)

but it doesn't work... And basic attacks don't works at all with this target eval.

Need help please !
Hi you,

Since you are using yanfly plugins, did you get a look at yanfly taunt plugin before ?

best,

Lay.
Yes, I'm using Tanfly taunt to perform the taunt. But taunt works only on target phase, not the turn when the taunt is performed on action phase. This is what I'm looking for : redirect attacks when the taunt move is used during action processing.
Understood,

I guess the script you proposed do not work because it is executed prior action phase. Thus, at this point, there isn't taunt effect yet : member.isStateAffected(5) will always be false (assuming state 5 is the taunt state).

An option is to change the battle system. Using a battle system with action executed right after selection, this problem will not be met (which is the case of all other battle system yanfly has made plugins for).
Keeping the default battle system, the only way I see is to use a passive state that will check before that the action connect if state 5 is applied on at least one of the foes. If yes, modify the target. This required yanfly Buff & State Core and yanfly Passive State.
There probably are other alternative but can't figure them out...

Good Luck,

Lay.
I found a solution ! You have to create a plugin that rewrite this method and all will be fine :

JavaScript:
if (Imported.YEP_Taunt){
    BattleManager.invokeNormalAction = function(subject, target) {
        var realTarget;
        var tauntTargets = this._action.getTauntTarget();

        if(tauntTargets && tauntTargets.length >= 1){
            realTarget = tauntTargets[Math.randomInt(tauntTargets.length)]
        }

        realTarget = this.applySubstitute(target);
        this._action.apply(realTarget);
        this._logWindow.displayActionResults(subject, realTarget);
    };
}

NB : this behavior is created to match with one of my skill who redirect all type of attack to the player with the state "Taunt". It includes Physical Taunt, Magical Taunt and Certain Taunt. It's possible to modify the code to enhance the behavior of the action redirection.

Original thread: https://forums.rpgmakerweb.com/threads/script-that-redirect-attack-on-an-ennemy-with-specific-state.121128/