MV Help retrieving target from enemy action

Im using Yanfly Buffs and States to create a set of states that lets battlers attack the same target in tandem with others based on which states they have in different situations. The states used by actors are the same as the ones used by enemies. While testing, i noticed that it works just as intended for actors but enemies attack at random. Each set of states is more or less written the same besides conditions and looks something like this:

State 1: (Has State Resist: State 3)
JavaScript:
<Custom Action Start Effect>
var action = user.currentAction();

if (action.isDamage() && action.isForOpponent()){
    user._commandTarget=action._targetIndex;

    for(var a=0;a < user.friendsUnit().aliveMembers().length;a++){
      
        if(user.friendsUnit().aliveMembers()[a].isStateAffected(3)){
            user.friendsUnit().aliveMembers()[a].addState(2);

        }
    }
}
</Custom Action Start Effect>

<Custom Remove Effect>
user._commandTarget=undefined;
</Custom Remove Effect>

State 2:
JavaScript:
<Custom Apply Effect>
BattleManager.queueForceAction(user, user._followerSkill || 137, origin._commandTarget);
user.removeState(2);
</Custom Apply Effect>

State 3: (has Resist State : State 1)
JavaScript:
Blank


This set of states makes it so that whenever a battler with state 1 makes an attack on an opponent, all allies with state 3 perform an attack on the original battler's target before the original battler makes their move, and then their move is used at the end. The user._commandTarget created whenever *actor* "X" with state 1 attacks a target returns what I want and in turn, it lets the actors with State 3 perform additional attacks on X's target before X's attack is made. However, when used by *enemy* "Y", the user._commandTarget created is -1, which makes the enemies with state 3 when Y attacks attack the party at random instead of being coordinated on the same target and ending with Y attacking the target.

Does anyone know of a way to get the correct target index or battler object off of Y's action before it goes off so i can pass it to the other enemies and make them all attack the same target before the skill actually executes on that target?
Bump
Bumpra
There are a number of ways you can do this.

In the Action Start Effect, you could store the current target on the current battler, user.hittingNow=target;
then in your Custom Apply Effect you can do
Code:
BattleManager.queueForceAction(user, user._followerSkill || 137, origin.hittingNow);

You could put it onto the following enemies when you give them the state:
Code:
        if(user.friendsUnit().aliveMembers()[a].isStateAffected(3)){
            user.friendsUnit().aliveMembers()[a].addState(2);
            user.friendsUnit().aliveMembers()[a].followTarget=target;
        }

then in the Custom Apply you'd do
Code:
BattleManager.queueForceAction(user, user._followerSkill || 137, user.followTarget);
Thank you for the reply. I appreciate it!
In the Action Start Effect, you could store the current target on the current battler, user.hittingNow=target;
then in your Custom Apply Effect you can do
Code:
BattleManager.queueForceAction(user, user._followerSkill || 137, origin.hittingNow);

I've tried something like this before, but part of the issue was that when "target" is used inside of <Custom Action Start Effect>, it returns the battler that the state is applied to instead of the target of that battler's actions.

Other effects like React and Confirm provided by the same plugin are capable of returning the skill's target through "target", but I wasn't able to get them to make the follower skills trigger before the commander made their move, and they had undesirable effects when used with multihit attacks
I've tried something like this before, but part of the issue was that when "target" is used inside of <Custom Action Start Effect>, it returns the battler that the state is applied to instead of the target of that battler's actions.
Oh, yes, that's not the right notetag.

You'd want to use something from inside the action flow, like Custom Initiate Effect.

they had undesirable effects when used with multihit attacks
Well you're doing kind of a very janky thing here 😛

You can put a boolean on the battlers (or flip a game switch) to make sure that any given bit of code is only executed once per entire action, then turn them back off in a Custom Conclude or Custom Regenerate or something.

You could also scrap all of this complexity and just fake it. Use a skill with action sequences to manipulate the other battlers, make it look like they're doing hits to contribute to the attack, then give them an invisible state with Cannot move so they don't take another action on their turn.
You can put a boolean on the battlers (or flip a game switch) to make sure that any given bit of code is only executed once per entire action, then turn them back off in a Custom Conclude or Custom Regenerate or something.
Oh? something like this?:
State 1:
JavaScript:
<Custom Initiate Effect>
if (action.isDamage() && action.isForOpponent()){
    for(var a=0;a < user.friendsUnit().aliveMembers().length;a++){
        user.friendsUnit().aliveMembers()[a]._ready=True;
        user._commandTarget=target;
    }
}
</Custom Initiate Effect>

<Custom Action End Effect>
for(var a=0;a < user.friendsUnit().aliveMembers().length;a++){
    if(user.friendsUnit().aliveMembers()[a]._ready && user.friendsUnit().aliveMembers()[a].isStateAffected(3)){
        user.friendsUnit().aliveMembers()[a]._ready=false;
        user.friendsUnit().aliveMembers()[a].addState(2);
    }
}
</Custom Action End Effect>

I can see that working well for a follow-up attack system. I might add that in as something on the side to the original and if more problems surface later, use it to replace the original and redo some encounter mechs with it in mind. But for now, it doesn't quite work out and i want to see if this is possible before having to rework it. I do quite like the idea as a whole though and will play around with it. Thank you for the idea!

The reason I'm looking for ways to pass the info before the "commander's" action is taken is because the "followers" would each have attacks that by themselves cannot kill a target, but would each chip away at the target's HP, decrease def, decrease element resistance, add poison stacks, etc depending on which follower it is and if they're alive. Then the commander tries to land the finishing blow after all the alive followers assist him in getting the kill. But above all, each follower still has to get their own natural turn each round. No disabling their turn when a commander is preparing for an attack.

I played around with it a while back and saw that i can influence it by making a boss take a turn to mark an actor with a state with +target rate. Then the following turn, all the allies would attack the same target and the boss would finish with their attack that also removes the +target rate state, but it came with the result of enemies being faster also being forced to attack that target too on their own turn.

You could also scrap all of this complexity and just fake it. Use a skill with action sequences to manipulate the other battlers, make it look like they're doing hits to contribute to the attack, then give them an invisible state with Cannot move so they don't take another action on their turn.

Ive made a few skills that make use of action sequences but not enough to know how to implement it with everything in mind. The first part is that i wanted any offensive skill the a commander uses to trigger the follower actions, not just a specific ones. So the action sequences route would need it to be changed so that the commander uses specific "trigger skills". Then each "trigger" skill itself would have to check to see which followers are alive on the commanders side to add additional effects to itself before execution correct? If i added the follower skill type as some kind of parameter to any battler that interacts with it, wouldn't the "trigger skill" have to check for each effect present and their count individually on the user's side to then apply the effects before the damage?

Sorry for what might be basic questions, but I'm not that familiar with action sequences and what can be done with them as a whole besides the few things i've already used them for (custom skill animations and changing skill scopes dynamically).
Oh? something like this?:
Yes, something like that, although you have a typo with True being capitalized - that will pop an undefined reference to a variable that doesn't exist.

I wouldn't bother putting the switch on every follower and looping through it again at the end...I'd put the switch on this attacking unit that's actually performing the action. Just a bit more efficient.

Ive made a few skills that make use of action sequences but not enough to know how to implement it with everything in mind. The first part is that i wanted any offensive skill the a commander uses to trigger the follower actions, not just a specific ones. So the action sequences route would need it to be changed so that the commander uses specific "trigger skills". Then each "trigger" skill itself would have to check to see which followers are alive on the commanders side to add additional effects to itself before execution correct? If i added the follower skill type as some kind of parameter to any battler that interacts with it, wouldn't the "trigger skill" have to check for each effect present and their count individually on the user's side to then apply the effects before the damage?
So I'm obviously not familiar with the skills in your game. But if you can make the skills your commander uses unique to that enemy (which should be very possible unless you have way too many skills in your database 😛), it's just a matter of using conditionals in the action sequence.

In kind of pseudo-code action sequence:
Code:
<target action>
if !$gameTroop.members()[1].isDead()
    motion swing: enemy 1
    action animation: target
    wait for animation
    action effect: target
end
if !$gameTroop.members()[2].isDead()
    motion swing: enemy 2
    action animation: target
    wait for animation
    action effect: target
end
motion swing: user
action animation: target
wait for animation
action effect: target
</target action>

But it's just another approach...if you get your existing system working and the overlapping states and forcing of actions doesn't screw with anything, then it's fine.
So I'm obviously not familiar with the skills in your game. But if you can make the skills your commander uses unique to that enemy (which should be very possible unless you have way too many skills in your database 😛), it's just a matter of using conditionals in the action sequence.

In kind of pseudo-code action sequence:
Code:
<target action>
if !$gameTroop.members()[1].isDead()
    motion swing: enemy 1
    action animation: target
    wait for animation
    action effect: target
end
if !$gameTroop.members()[2].isDead()
    motion swing: enemy 2
    action animation: target
    wait for animation
    action effect: target
end
motion swing: user
action animation: target
wait for animation
action effect: target
</target action>

But it's just another approach...if you get your existing system working and the overlapping states and forcing of actions doesn't screw with anything, then it's fine.
Thank you! the logic is actually a lot simpler than what I initially had in mind and would probably work better than what I would have stitched together. It looks like it would likely work more or less as I intended with a little extra fooling around for other game mechanics. In the event that I cant get the original to work with some other methods, that looks like that will function just fine as a failsafe

Original thread: https://forums.rpgmakerweb.com/threads/help-retrieving-target-from-enemy-action.154901/