MV checking the target of a skill on <custom action start effect>

My project uses a number of Yanfly plugins, and using some of them, I was trying to recreate the "Cover" effect from the final fantast series. What it does in FF is that the user manually selects a ally to cover and whenever that target is attacked, the user intercepts the attack and takes it instead as if they were its target, also using their stats for the damage calculation. Its similar to the substitute effect, but unlike substitute, it only effects the one selected ally and the ally does not need to be at low hp to trigger.

the way I was trying to go about it was that attacks meant for the cover target would be entirely redirected to the user instead of intercepting the attack. If anyone has any pointers on how this can be better implemented it would be appreciated.

I already have a weaker version of cover i called conceal that redirects half of the damage taken by the target to the user through custom react effects. But it only redirects a portion of the damage. It doesnt recalculate the skill's damage to check what the guardian would have taken with their defense stats. It doesnt give the guardian any of the ailments/debuffs the skill would have given the target. It just pushes half of the damage the target took to the guardian.
I wanted Cover to be different in that the entire attack essentially gets retargeted to the cover user, special effects and all.

I had the effect broken into 3 states. This is the note for the skill itself.

Code:
<After Eval>
user._ward = target;
user.addState(458);
target.addState(459);
</After Eval>

<Cooldown: 8>

<Select Conditions>
Not User
</Select Conditions>

This one goes on the guardian when cover is used.
Code:
<Custom Remove Effect>
user._ward.removeState(stateId+1);
user._ward = undefined;
</Custom Remove Effect>

<Alive Foe Aura: 460>

This one goes on the target of the cover skill:
Code:
<Custom Apply Effect>
user._guardian = origin;
</Custom Apply Effect>

<Custom Remove Effect>
user._guardian.removeState(stateId-1);
user._guardian = undefined;
</Custom Remove Effect>

and this one is applied to all enemies through the aura on the first state. it was supposed to carry out the effect.
Code:
<Custom Action Start Effect>
var action = user.currentAction();
if(action.isForOpponent() && action.isDamage() && action.isForOne()){
  if(target.isStateAffected(stateId-1)){
    action.setTarget(target._guardian.index());
  }
}
</Custom Action Start Effect>

The problem here is that I hadnt realized that the "target" within <custom action start effect> returns the user currently acting, not the target of the skill being used. I tried using the currentAction() of the user to get the skill and then tried to check the target through the skill's ._targetIndex, but its always -1. i cant use the index to check if the attack is actually aimed at the covered actor in order to redirect it. Any ideas as to how to continue with this?
You may find this easier to implement with a variety of existing plugins that allow you to customize Substitute.

That being said...I think you have the right idea. I probably would've skipped the aura and just put it on the ward's state as a Custom Select Effect, but I don't see why this can't work.

This one goes on the guardian when cover is used.
Code:
<Custom Remove Effect>
user._ward.removeState(stateId+1);
user._ward = undefined;
</Custom Remove Effect>
Just as a very minor point, it's slightly better practice to delete user._ward than to set it to undefined. A variable on an actor that's undefined will still be written into the save file and maintained, whereas delete actually removes it when you're done.

It's a matter of bytes, just offering the tidbit 🙂

The problem here is that I hadnt realized that the "target" within <custom action start effect> returns the user currently acting, not the target of the skill being used. I tried using the currentAction() of the user to get the skill and then tried to check the target through the skill's ._targetIndex, but its always -1.
Did you try going through the battle manager?
Code:
BattleManager._targets.forEach(foe => {if (foe.isStateAffected(stateId-1)) target=foe);
if (target!=user)
    action.setTarget(target._guardian.index());

Oh, you already checked it's for one target and all that. So you could just do:
Code:
target=BattleManager._targets[0];
and go from there with checking the state.
You may find this easier to implement with a variety of existing plugins that allow you to customize Substitute.
I had considered putting something together to edit the substitute check but i was reluctant to go through with it. Substitute is actually being used within a couple states. its not very frequently used, but since it is being used as is, i wanted to stay away from anything that would alter its behavior.

That being said...I think you have the right idea. I probably would've skipped the aura and just put it on the ward's state as a Custom Select Effect, but I don't see why this can't work.
I did try putting it in the ward state through a select effect, but i didnt have any luck with it. admittedly, after the first attempt it didnt take very long for me to ditch it in favor of the aura idea. I had assumed it was a timing thing where by the time a select effect were to occur its too late to change the target and just moved on.
Did you try going through the battle manager?
Code:
target=BattleManager._targets[0];

Ill be honest here, never occurred to me to check there. I had incorrectly assumed that the the currentAction() got it's target index through the battlemanager and never looked any further. This is good to know. i dont very often use battlemanager aside from forcing actions so i wasnt aware that it had the actual target info available.

Having made the suggested change It seems to partially work? ive gone through a few iterations to try to see if it was just how i wrote it that was causing issues. theres an odd interaction going on here that i cant seem to pinpoint the cause of. The test setup is 2 actors against an enemy that only has the standard attack.
After changing the aura's state to:

Code:
<Custom Action Start Effect>
var action = user.currentAction();
var T = BattleManager._targets[0];
console.log(T);

if(action.isForOpponent() && action.isDamage() && action.isForOne()){
  if(T.isStateAffected(stateId-1)){
    console.log("Attack is valid for redirection");
    action.setTarget(T._guardian.index());
  }
}
</Custom Action Start Effect>
(I had also added console output as a react effect to a guardian and a ward being hit for my sake at some point)
1693322472808.png

I have the ward do nothing but pass their turns, the guardian uses cover turn 1 and then passes the rest of theirs. The first attack is initially directed towards the ward (i gave them target tate+1000%). Its successfully redirected towards the guardian. Following attacks seem to just be going wild? on the second attack, T initially returns as the enemy itself using the attack, bypasses the cover and kills the ward. the third initially just targets the dead ward and then hits the remaining guardian. I cant think of anything that could be causing this

i then just made a passive for that enemy. all it does is print out to the console what BattleManager._targets[0] is at the start of their action.
1693323052040.png

Stepping away from the cover states entirely, the passive gives me this.
1693325235472.png

which aside from the first hit actually landing and killing the ward character and the 2nd attack being for the guardian as theyre the only alive actor at that point, is correctly updating past that point

When i have more time i'll test it out further in a fresh project if needs be, but is this abnormal behavior or am i just overlooking something that should be obvious? is Action Start too early to be able to properly use the battle manager?

Just as a very minor point, it's slightly better practice to delete user._ward than to set it to undefined. A variable on an actor that's undefined will still be written into the save file and maintained, whereas delete actually removes it when you're done.
Also, noted!

Original thread: https://forums.rpgmakerweb.com/threads/checking-the-target-of-a-skill-on-custom-action-start-effect.160723/