MV How to make a state that redirects actions to other targets?

Basically I want to make a "frightened" state that makes it so the user of the state cannot use direct skills against the enemy that gave them the state. Well, they can target and use skills against the origin of the state, but these skills should be redirected to another valid target as soon as they are casted(this is to make it consistent with the taunt state I'm using). If there are no valid targets apart from the origin of the frightened state, the skill should work as usual. I'm using most of yanfly's plugins (most relevant are buffs and states core and taunt plugins). This is the notetag used for the taunt state:


<Custom Action Start Effect>

// Get current action.

var action = user.currentAction();

// Check if action exists, for an opponent, and that the provoker isn't on the same team

if (action && action.isForOpponent() && origin.isActor() !== user.isActor()) {

action.setTarget(origin.index());

}

</Custom Action Start Effect>


Thanks a lot for reading!!!
Well if that code for taunt state worked, then maybe something like this (with same notetag):

var action = user.currentAction();
if (action && action.isForOpponent() && action.isForOne() && action._targetIndex === origin.index() && $gameTroop.aliveMembers().length > 1){
var index = $gameTroop.aliveMembers().indexOf(origin);
var validTargets = $gameTroop.aliveMembers().toSpliced(index, 1);
var newTarget = validTargets[Math.randomInt(validTargets.length)];
action.setTarget(newTarget.index());
}

Not 100% sure if you can get the action target like that with that notetag.
iirc, that'd work for actors but not enemies. enemies dont target actors the same ways actors target enemies. When they pick a target, its usually done at random and the target index is -1. might have to change the condition to also force the target if the target index is -1, but im not sure if that would mess with the AI or mess up "x random enemies" scope skills
Ok cool, I was only thinking of actors. If they want it to work for enemies too, they'd also have to change the reference from gameTroop to gameParty depending on who is affected.
they'd also have to change the reference from gameTroop to gameParty depending on who is affected
im so used to using user.friendsUnit() and oponentsUnit() instead of $gameTroop and $gameParty that I completely overlooked that 🤣
First of all, thanks a lot to all of you for answering my question. Thanks for the code provided by rpgLord69 I could make the "frightened" state work for actors. Someone from reddit helped me modify it so it can work properly.


<Custom Action Start Effect>

// Get current action

var action = user.currentAction();



// Check if action exists, is for a single opponent, and targets the origin of this state

if (action && action.isForOpponent() && action.isForOne() &&

origin && origin.index() === action._targetIndex &&

origin.isEnemy() !== user.isEnemy()) {



// Get the opposing unit

var unit = origin.isEnemy() ? $gameParty : $gameTroop;

var aliveMembers = unit.aliveMembers();



// Remove the origin from potential targets

var validTargets = aliveMembers.filter(function(member) {

return member !== origin;

});



// If there are other valid targets, choose one randomly

if (validTargets.length > 0) {

var newTarget = validTargets[Math.floor(Math.random() * validTargets.length)];

action._targetIndex = newTarget.index();



// For taunt compatibility, we need to update the action's target selection

action._targets = null; // This will force the action to recalculate targets

}

}

</Custom Action Start Effect>

As I said before, it works only for player-controlled actors. For enemies a different approach is necessary. I think it'd better to include yanfly's target core and selection control plugins.

Original thread: https://forums.rpgmakerweb.com/threads/how-to-make-a-state-that-redirects-actions-to-other-targets.179734/