MZ Help with a Counterattack State.

EDIT: That's what I managed to came up with. I know it's not "hit who hit you", but -2 still allows you to choose you who you want to hit, so, if no other way is found, I'm plenty satisfied with this one (because it works with FTB and can use custom skills)
You can to post a reply if youre not the last person to have done so. We dont get notifications when you edit a reply. We're even less likely notice any edits made when yours is no longer the last one in the thread.

what you have works if your counter state will only ever be usable on member[1] and you no longer want it to be a counter directed towards the attacker, but if you continue it like that youll have to make different ones for every member position and enemy position that can have one for every skill that can be used as a counter.
If your member[0] had that state and were to be attacked, member[1] would counter for them
You can to post a reply if youre not the last person to have done so. We dont get notifications when you edit a reply. We're even less likely notice any edits made when yours is no longer the last one in the thread.

what you have works if your counter state will only ever be usable on member[1] and you no longer want it to be a counter directed towards the attacker, but if you continue it like that youll have to make different ones for every member position and enemy position that can have one for every skill that can be used as a counter.
If your member[0] had that state and were to be attacked, member[1] would counter for them
I just prefer safe than sorry, since it seems like multi posting is treated harshly there, and, well, it's not like I added much to last post anyway.
And about state, I fail to see a problem, since this counter skill is available only for one member, and he isn't supposed to protect others anyway (and if I wanted skill that makes them take hits for other actor, I'd rather just put taunt on them through VisuMZ Aggro plugin)
Also I don't really get problem with enemy position. With -2 it by deffault targets random enemy (I guess), and if you hit enemy, it targets that enemy now. Seems to work pretty well, considering only other options are either "target random enemy" or "enemy troop ID", which both are much less favorable (no control at all/coding nightmare)
since this counter skill is available only for one member, and he isn't supposed to protect others anyway
The issue i meant to focus on isnt so much that only that actor can use it, its that the state chooses by party position. if for any reason the actor you intend to counter with with isnt member[1], its still going to be member[1] to perform the counter anyways. that actor can be party slot 1, but party slot 2 will attack when party slot 1 gets hit if you were to swap their positions with in the party. if youre going to structure the game where the player cannot rearrange them and the game wont ever do so either, then its not an issue, but otherwise that may be an oversight.

only other options are either "target random enemy" or "enemy troop ID"
thats what the references for the tags are for. when the battler with that state is hit, a variable is automatically created within the tag that youre usng to allow you to refer to the attacker automatically. you can then use that reference as the target for the forced action. Id look at the entry for the tag youre using to find it. the one you posted in that 2nd edit isnt the same tag. thats a for skills, not states
The issue i meant to focus on isnt so much that only that actor can use it, its that the state chooses by party position. if for any reason the actor you intend to counter with with isnt member[1], its still going to be member[1] to perform the counter anyways. that actor can be party slot 1, but party slot 2 will attack when party slot 1 gets hit if you were to swap their positions with in the party. if youre going to structure the game where the player cannot rearrange them and the game wont ever do so either, then its not an issue, but otherwise that may be an oversight.


thats what the references for the tags are for. when the battler with that state is hit, a variable is automatically created within the tag that youre usng to allow you to refer to the attacker automatically. you can then use that reference as the target for the forced action. Id look at the entry for the tag youre using to find it. the one you posted in that 2nd edit isnt the same tag. thats a for skills, not states
I didn't really found anything about targetting in VisuMZ SkillStates (that's why I was doubting skill target notetag wouldn't work)
Edit: About last post, I do it just in case. I'm not bothered with it, I just don't want to bother other.
I just prefer safe than sorry, since it seems like multi posting is treated harshly there
What makes you think that? If a person double posts, they get a reminder that they're not supposed to. How is that harsh? Does it injure you to see blue text? 🤣

And your last edit was after two other people posted, so it would not have been a double post, which is what @Robro33 already said.

If you feel you're confused about the forum rules, you can just ask for an explanation.
Apologies. This took me a few minutes to work out, but I'm necroposting for the sake of hopefully helping others because Google always points searchers of this topic here.

When using this idea for monsters counter-attacking, I ran into problems. The solution I had (using Visustella Battle Core and Auto Skill Triggers) was to modify the Damage Algorithm to store the index of the user in a game variable.

Auto Skill Triggers selects a random target among the valid ones. This set up makes it a guaranteed counter attack against the attacker (barring some janky logic error). I also think that using the Visustella ABS System might mess with some of the above, which is why I was struggling to get it properly working.

The game I'm working on has no player counter attacks, so I

JavaScript:
if (user.isActor())
 {$gameVariables.setValue(1, user.index());}

Then, modifying it to fit Neo Soul Gamer's code above (NSG's code was genius)...

JavaScript:
// Set skill as a counter attack
<Auto Trigger: Attack Target>

<JS Targets>
// Clears all targets
targets = [];

// Get the index of the last actor to attack as stored in damage algorithm
let eIndex = $gameVariables.value(1);

// Adds the actor that acted last to the pool of available targets
targets.push($gameParty.members()[eIndex])

</JS Targets>

Monster counter attacks the correct attacker every time now.
I tried this one but it didnt work. maybe i didnt set it up correctly but the enemies keep attacking index 0 in the player party

Code:
<JS Targets>
if (user.isActor())
{$gameVariables.setValue(1, user.index());}
targets = [];
let eIndex = $gameVariables.value(1);
targets.push($gameParty.members()[eIndex])
</JS Targets>

<Hide in Battle>
<Auto Trigger: Attack Target>
I tried this one but it didnt work. maybe i didnt set it up correctly
You didn't. Putting those first two lines of codes inside the JS Targets notetag is saying "store the index of this battler if it's an actor."

So if it's an actor, then you're saying the only valid target is itself.

And if it's an enemy, those lines will never be true and you're using variable 1 when it's undefined (thus index 0).

If you look at the post you quoted, those lines are not inside the JS Targets notetag.
I have tried using this code before but they still never target the correct player battler

Code:
<JS Targets>
{$gameVariables.setValue(1, user.index());}
targets = [];
let eIndex = $gameVariables.value(1);
targets.push($gameParty.members()[eIndex])
</JS Targets>

<Hide in Battle>
<Auto Trigger: Attack Target>

As i understand it stores the User variable on the variable 1, then clears the targets and after that it selects the value of the varibale 1 previusly stored. Lastly it makes the enemy attack that index but i might be doing something wrong. The enemy has the skill so they can counter attack the player and it activates when reciving damage from the player party.
As i understand it stores the User variable on the variable 1
The user here is the battler using the counter skill, so you do not want any part of that to be involved in what they're targeting.

A much easier way to get the actor who attacked is with the last game data that's built into MZ.

Code:
targets = [$gameActors.actor($gameTemp.lastActionData(2))];
The user here is the battler using the counter skill, so you do not want any part of that to be involved in what they're targeting.

A much easier way to get the actor who attacked is with the last game data that's built into MZ.

Code:
targets = [$gameActors.actor($gameTemp.lastActionData(2))];
That was so easy thank you!
This is all it needs to work as intended.


Code:
<JS Targets>
targets = [$gameActors.actor($gameTemp.lastActionData(2))];
</JS Targets>
<Auto Trigger: Attack Target>
I know this thread is over two years old, but I just wanted to make a quick post to thank @NaosoX for the help. I was trying to solve this problem with my own counter skill. I was able to do this without using states with just a base passive skill, but it was your idea to find the enemy id via variable that I wasn't aware of that got me past two days of work trying to get this right. Thanks so much for your help.
Yeah Nao is great

Original thread: https://forums.rpgmakerweb.com/threads/help-with-a-counterattack-state.142623/