MV How can I grab the action AND the target??? (Yanfly's Buffs & States Core)

Ok, so there's this passive skill that randomly makes one of the party members receive an attack instead of the leader. I'm doing this by using 2 states: one for the party leader (I need this to make the skill work only when the player has learned that passive skill), and another for all the enemies in the battle (I need this to check whether they'll be targeting the party leader and redirect it to someone else).

So, the leader state is used to apply the other state on all enemies as soon as the battle starts. But I'm having trouble to make the enemy state work, since I can grab their actions during <Custom Action Start Effect> (with user.currentAction()) but not their target, and during <Custom Initiate Effect> I can grab their target but not their action and therefore I can't change their target.

Can someone help me with this please? 🙁
during <Custom Initiate Effect> I can grab their target but not their action and therefore I can't change their target.
In an Initiate Effect, the action is this...
@ATT_Turan okay, that actually makes sense... You have to use "currentAction()" during the custom timing effects because it precedes the battler's action... And you can use "this" during the custom action effects because the action is already taking place, am I correct?

Anyway can you please give me a light concerning another issue? Is it possible to change that action's target during <Custom Initiate Effect>? Looks like I can't set the "target" variable here, and "setTarget" doesn't seem to work either (maybe because I'm not using it correctly).

In short, during <Custom Initiate Effect> I want to check if the target is the party leader, and if true, and I want to redirect that attack to another random ally. Thanks in advance!
You have to use "currentAction()" during the custom timing effects because it precedes the battler's action... And you can use "this" during the custom action effects because the action is already taking place, am I correct?
No, not really. this is a basic part of JavaScript syntax, and it's used in this case because the various action effects are methods of the Game_Action class. The code is, in effect, being run by the action.

In short, during <Custom Initiate Effect> I want to check if the target is the party leader, and if true, and I want to redirect that attack to another random ally. Thanks in advance!
This seems like such a harder way to do it than you need to - why not just use target/selection tags to force the action to select not the party leader in the first place? Or Battle A.I., presuming this is only being done by enemies?

You could even apply a state with Taunt to everyone except the party leader...it just seems like trying to use JavaScript to manipulate the target of the action while it's being executed is choosing the most complex solution possible.
@ATT_Turan that's because I'm supposed to code a passive skill for the party leader that works when they're targeted by an enemy attack or skill. This passive skill adds a small chance for one of the allies to receive the attack instead of the party leader. That's why I'm using states: because it's a passive skill, it needs a passive state. The game has more than 100 skills, and half of these skills are passive (and this is the only one left).

My biggest problem is the timing for activating that passive skill, cause first I must check if the party leader is the one being targeted, then I must check if the passive skill is actually triggered (since it's a random chance) and if it is, then I must randomly choose one of the other allies and set them as the target. (I'm not the one coming up with these skills by the way, I'm only supposed to code them.)

I tried doing this by using only the party leader's passive state with a <Custom Select Effect> but this tag doesn't allow me to manipulate the enemy's actions. So, instead I made the party leader's passive state apply a hidden state on all the enemies as soon as the battle starts, so I have access to their actions.

Also, I shouldn't code this into the enemy's or the skill's notetag, cause there are multiple enemies who can use multiple skills, and I'd have to copy & paste the same code for all of them, since the party leader's passive skill takes all kind of offensive actions into consideration.
@ATT_Turan that's because I'm supposed to code a passive skill for the party leader that works when they're targeted by an enemy attack or skill. This passive skill adds a small chance for one of the allies to receive the attack instead of the party leader.
So why not use one of the handful of plugins that controls how actors substitute for a party member during an incoming attack? That sounds like exactly what you're trying to do.

Also, how the heck could the player tell the difference between "an enemy targeted the leader and code shifted it to another actor" and "an enemy targeted another actor in the first place"?

Also, I shouldn't code this into the enemy's or the skill's notetag, cause there are multiple enemies who can use multiple skills, and I'd have to copy & paste the same code for all of them, since the party leader's passive skill takes all kind of offensive actions into consideration.
Why not? It's one of the things I would personally consider doing. This thread has been going since Monday, and I feel like it's an extension of the same question you were asking last week or longer ago.

It would take, like, a couple of minutes to copy and paste a notetag into all of your enemy skills.

Original thread: https://forums.rpgmakerweb.com/threads/how-can-i-grab-the-action-and-the-target-yanflys-buffs-states-core.173162/