MZ Help with a Counterattack State.

Hello, I am trying to create a counter-attack state, but can't seem to get things working right.

I want the character to actually take the attack before counterattacking, instead of the default dodge in vanilla MZ. I'm using Visustella Plugins (specifically Auto Skill Triggers) to do this. First I create a state that gives the character the skill, so that when the are hit by a physical attack the character takes the hit then counterattacks. This works but with one problem, the attacker simply attacks a random target instead of enemy that attacked.
1638329428492.png


1638329297816.png


Is there any way to control the counterattack target, or do I have to approach this a different way?
Unless there are any settings in the plugin parameters, the Auto Skills plugin doesn't allow any kind of targeting with the notetags.

Yanfly wrote a Counter Control plugin for MV, but I don't see anything by VisuStella that reproduces it. Perhaps it works via FOSSIL?

The other approach I thought of was Yanfly's states plugin had lots of tags for code to be executed at different times of the battle turn (custom respond effects, etc.) but, again, I'm not seeing any equivalents in the VisuStella states plugin.
Thanks for the reply.

Well that's a pain. Can I do anything with the Javascript notetags? I not exactly literate with Javascripting, but I can get my head around the simple stuff with a bit of trial and error.

Fossil seems like an interesting idea. I dabbled with MV and Yanfly back with my old computer, but that was long enough ago that I thought starting from scratch with MZ would be a better idea. Does anyone know if it clashes with the MZ Visustella plugins?

I did think about doing the counterattacks with common events, might have to give that a shot. If that doesn't work then I'll look into FOSSIL.
You wanted something like this?


Spoiler: State Activation Skill
StateActivation.png

Skill which adds state

Spoiler: Counter State
CounterState.png

This state adds the skill Counter Attack

Spoiler: Counter Attack Skill
CounterSkill.png

Skill added by state, hidden in battle.
Auto Skill Trigger set.
Linked to Common Event: CounterAttack

There are a few ways to do this, here is one I think you can customise to your liking.

Spoiler: Counter Attack Common Event
Setting variable from here will add 1, so to counter act this effect, Control Variable again, but subtract 1.

Code:
◆Control Variables:#0044 Counter Target = Last Enemy Index to Act
◆Control Variables:#0044 Counter Target -= 1
◆Plugin Command:VisuMZ_1_BattleCore, ACSET: Setup Action Set
:              :Display Action = true
:              :Immortal: On = true
:              :Battle Step = true
:              :Wait For Movement = true
:              :Cast Animation = true
:              :Wait For Animation = true
◆If:Counter Target = 0
  ◆Plugin Command:VisuMZ_1_BattleCore, MOVE: Move To Target(s)
  :              :Targets (Moving) = ["user"]
  :              :Targets (Destination) = ["enemy index 0"]
  :              :Target Location = front base
  :              :Melee Distance = 24
  :              :Offset Adjustment = horz
  :              :Offset: X = 0
  :              :Offset: Y = 0
  :              :Duration = 12
  :              :Face Destination? = true
  :              :Movement Easing = Linear
  :              :Movement Motion = walk
  :              :Wait For Movement? = true
  ◆Plugin Command:VisuMZ_1_BattleCore, MOTION: Motion Type
  :              :Targets = ["user"]
  :              :Motion Type = attack
  :              :Show Weapon? = true
  ◆Wait:30 frames
  ◆Plugin Command:VisuMZ_1_BattleCore, ANIM: Action Animation
  :              :Targets = ["enemy index 0"]
  :              :Mirror Animation = false
  :              :Wait For Animation? = true
  ◆Plugin Command:VisuMZ_1_BattleCore, MECH: Action Effect
  :              :Targets = ["enemy index 0"]
  ◆
:End
◆If:Counter Target = 1
  ◆Plugin Command:VisuMZ_1_BattleCore, MOVE: Move To Target(s)
  :              :Targets (Moving) = ["user"]
  :              :Targets (Destination) = ["enemy index 1"]
  :              :Target Location = front base
  :              :Melee Distance = 24
  :              :Offset Adjustment = horz
  :              :Offset: X = 0
  :              :Offset: Y = 0
  :              :Duration = 12
  :              :Face Destination? = true
  :              :Movement Easing = Linear
  :              :Movement Motion = walk
  :              :Wait For Movement? = true
  ◆Plugin Command:VisuMZ_1_BattleCore, MOTION: Motion Type
  :              :Targets = ["user"]
  :              :Motion Type = attack
  :              :Show Weapon? = true
  ◆Wait:30 frames
  ◆Plugin Command:VisuMZ_1_BattleCore, ANIM: Action Animation
  :              :Targets = ["enemy index 1"]
  :              :Mirror Animation = false
  :              :Wait For Animation? = true
  ◆Plugin Command:VisuMZ_1_BattleCore, MECH: Action Effect
  :              :Targets = ["enemy index 1"]
  ◆
:End
◆If:Counter Target = 2
  ◆Plugin Command:VisuMZ_1_BattleCore, MOVE: Move To Target(s)
  :              :Targets (Moving) = ["user"]
  :              :Targets (Destination) = ["enemy index 2"]
  :              :Target Location = front base
  :              :Melee Distance = 24
  :              :Offset Adjustment = horz
  :              :Offset: X = 0
  :              :Offset: Y = 0
  :              :Duration = 12
  :              :Face Destination? = true
  :              :Movement Easing = Linear
  :              :Movement Motion = walk
  :              :Wait For Movement? = true
  ◆Plugin Command:VisuMZ_1_BattleCore, MOTION: Motion Type
  :              :Targets = ["user"]
  :              :Motion Type = attack
  :              :Show Weapon? = true
  ◆Wait:30 frames
  ◆Plugin Command:VisuMZ_1_BattleCore, ANIM: Action Animation
  :              :Targets = ["enemy index 2"]
  :              :Mirror Animation = false
  :              :Wait For Animation? = true
  ◆Plugin Command:VisuMZ_1_BattleCore, MECH: Action Effect
  :              :Targets = ["enemy index 2"]
  ◆
:End
◆If:Counter Target = 3
  ◆Plugin Command:VisuMZ_1_BattleCore, MOVE: Move To Target(s)
  :              :Targets (Moving) = ["user"]
  :              :Targets (Destination) = ["enemy index 3"]
  :              :Target Location = front base
  :              :Melee Distance = 24
  :              :Offset Adjustment = horz
  :              :Offset: X = 0
  :              :Offset: Y = 0
  :              :Duration = 12
  :              :Face Destination? = true
  :              :Movement Easing = Linear
  :              :Movement Motion = walk
  :              :Wait For Movement? = true
  ◆Plugin Command:VisuMZ_1_BattleCore, MOTION: Motion Type
  :              :Targets = ["user"]
  :              :Motion Type = attack
  :              :Show Weapon? = true
  ◆Wait:30 frames
  ◆Plugin Command:VisuMZ_1_BattleCore, ANIM: Action Animation
  :              :Targets = ["enemy index 3"]
  :              :Mirror Animation = false
  :              :Wait For Animation? = true
  ◆Plugin Command:VisuMZ_1_BattleCore, MECH: Action Effect
  :              :Targets = ["enemy index 3"]
  ◆
:End
◆If:Counter Target = 4
  ◆Plugin Command:VisuMZ_1_BattleCore, MOVE: Move To Target(s)
  :              :Targets (Moving) = ["user"]
  :              :Targets (Destination) = ["enemy index 4"]
  :              :Target Location = front base
  :              :Melee Distance = 24
  :              :Offset Adjustment = horz
  :              :Offset: X = 0
  :              :Offset: Y = 0
  :              :Duration = 12
  :              :Face Destination? = true
  :              :Movement Easing = Linear
  :              :Movement Motion = walk
  :              :Wait For Movement? = true
  ◆Plugin Command:VisuMZ_1_BattleCore, MOTION: Motion Type
  :              :Targets = ["user"]
  :              :Motion Type = attack
  :              :Show Weapon? = true
  ◆Wait:30 frames
  ◆Plugin Command:VisuMZ_1_BattleCore, ANIM: Action Animation
  :              :Targets = ["enemy index 4"]
  :              :Mirror Animation = false
  :              :Wait For Animation? = true
  ◆Plugin Command:VisuMZ_1_BattleCore, MECH: Action Effect
  :              :Targets = ["enemy index 4"]
  ◆
:End
◆If:Counter Target = 5
  ◆Plugin Command:VisuMZ_1_BattleCore, MOVE: Move To Target(s)
  :              :Targets (Moving) = ["user"]
  :              :Targets (Destination) = ["enemy index 5"]
  :              :Target Location = front base
  :              :Melee Distance = 24
  :              :Offset Adjustment = horz
  :              :Offset: X = 0
  :              :Offset: Y = 0
  :              :Duration = 12
  :              :Face Destination? = true
  :              :Movement Easing = Linear
  :              :Movement Motion = walk
  :              :Wait For Movement? = true
  ◆Plugin Command:VisuMZ_1_BattleCore, MOTION: Motion Type
  :              :Targets = ["user"]
  :              :Motion Type = attack
  :              :Show Weapon? = true
  ◆Wait:30 frames
  ◆Plugin Command:VisuMZ_1_BattleCore, ANIM: Action Animation
  :              :Targets = ["enemy index 5"]
  :              :Mirror Animation = false
  :              :Wait For Animation? = true
  ◆Plugin Command:VisuMZ_1_BattleCore, MECH: Action Effect
  :              :Targets = ["enemy index 5"]
  ◆
:End
◆If:Counter Target = 6
  ◆Plugin Command:VisuMZ_1_BattleCore, MOVE: Move To Target(s)
  :              :Targets (Moving) = ["user"]
  :              :Targets (Destination) = ["enemy index 6"]
  :              :Target Location = front base
  :              :Melee Distance = 24
  :              :Offset Adjustment = horz
  :              :Offset: X = 0
  :              :Offset: Y = 0
  :              :Duration = 12
  :              :Face Destination? = true
  :              :Movement Easing = Linear
  :              :Movement Motion = walk
  :              :Wait For Movement? = true
  ◆Plugin Command:VisuMZ_1_BattleCore, MOTION: Motion Type
  :              :Targets = ["user"]
  :              :Motion Type = attack
  :              :Show Weapon? = true
  ◆Wait:30 frames
  ◆Plugin Command:VisuMZ_1_BattleCore, ANIM: Action Animation
  :              :Targets = ["enemy index 6"]
  :              :Mirror Animation = false
  :              :Wait For Animation? = true
  ◆Plugin Command:VisuMZ_1_BattleCore, MECH: Action Effect
  :              :Targets = ["enemy index 6"]
  ◆
:End
◆If:Counter Target = 7
  ◆Plugin Command:VisuMZ_1_BattleCore, MOVE: Move To Target(s)
  :              :Targets (Moving) = ["user"]
  :              :Targets (Destination) = ["enemy index 7"]
  :              :Target Location = front base
  :              :Melee Distance = 24
  :              :Offset Adjustment = horz
  :              :Offset: X = 0
  :              :Offset: Y = 0
  :              :Duration = 12
  :              :Face Destination? = true
  :              :Movement Easing = Linear
  :              :Movement Motion = walk
  :              :Wait For Movement? = true
  ◆Plugin Command:VisuMZ_1_BattleCore, MOTION: Motion Type
  :              :Targets = ["user"]
  :              :Motion Type = attack
  :              :Show Weapon? = true
  ◆Wait:30 frames
  ◆Plugin Command:VisuMZ_1_BattleCore, ANIM: Action Animation
  :              :Targets = ["enemy index 7"]
  :              :Mirror Animation = false
  :              :Wait For Animation? = true
  ◆Plugin Command:VisuMZ_1_BattleCore, MECH: Action Effect
  :              :Targets = ["enemy index 7"]
  ◆
:End
◆Plugin Command:VisuMZ_1_BattleCore, MOVE: Home Reset
:              :Targets = ["user"]
:              :Wait For Movement? = true
◆Plugin Command:VisuMZ_1_BattleCore, ACSET: Finish Action
:              :Immortal: Off = true
:              :Wait For New Line = true
:              :Wait For Effects = true
:              :Clear Battle Log = true
:              :Home Reset = true
:              :Wait For Movement = true
Thanks, I'll see if that works.
Moved to: Plugin Support
Thanks so much. Also, sorry for double-posting.

It works perfectly. I'll admit I had a bit of a mix up with the enemy index/ID in the plugin commands, and was left wondering why the countering character disappeared entirely and only countered certain enemies (whose shared an ID/Index number). But after fixing that, everything worked just fine.

I was also concerned that multiple actors having the skill at the same time would cause problems, as well as if party-wide attacks would cause problems either (and obviously both at once). But it works perfectly in all three situations.
Im glad you enjoy it 😀
I hate to Necropost, but this thread can be really useful for others looking for this solution, so I wanted to offer a cleaner way of pulling this off.

In the counter attack skill, you can use the following code from VS Battle Core:
JavaScript:
<JS Targets>
// Clears all targets
targets = [];
// Get the index of the last enemy to attack
let eIndex = $gameTemp._lastActionData[3] - 1;
// Adds the enemy that acted last to the pool of available targets
targets.push($gameTroop.members()[eIndex])
</JS Targets>
What this does is override the original scope of the skill so that only the last enemy to attack is added as an available target. Now you can simply set your action sequence targets as "all targets", removing the need for conditional branches to check for every possible index.
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 to make skill like was shown in NaosoX guide, but it just doesn't work for me. Can someone tell me what I did wrong?
Not exactly sure how that one is supposed to work because I'm not using common events, I just use JS in the note textbox of the actual counterattack skill. I'll post what I've done.

You'll need Yanfly's Auto Skill Triggers and the Battle Core for it to work.

Here's the initial skill to add the counterattack state to the actor. Kind of straightforward.

Screenshot 2023-12-01 105834.png


And the State itself.
Screenshot 2023-12-01 105913.png


And finally the actual counter move itself.
Screenshot 2023-12-01 105857.png


The JS in text box. The switch weapon type is for a different plugin, so you can remove it and it won't make a difference. This counter is only supposed to work with physical attacks, but you can just change the auto trigger to whatever you want it to be.

<Hide in Battle>
<Auto Trigger: Physical Enemy>
<Switch to Weapon Type: 7>

<JS Targets>
// Clears all targets
targets = [];
// Get the index of the last enemy to attack
let eIndex = $gameTemp._lastActionData[3] - 1;
// Adds the enemy that acted last to the pool of available targets
targets.push($gameTroop.members()[eIndex])
</JS Targets>

This works for me. Hope this helps.
Thanks, but I already found source of issue. FTB simply doesn't work with Auto Trigger, so I have to find a way around now.
Thanks, but I already found source of issue. FTB simply doesn't work with Auto Trigger, so I have to find a way around now.
oh no! that's why. What are your ideas?
oh no! that's why. What are your ideas?
I didn't tried that, but what about "force action" script call and "post damage" notetag? Now, how would I make actor actually target the enemy which hit them with setup like that?
I didn't tried that, but what about "force action" script call and "post damage" notetag? Now, how would I make actor actually target the enemy which hit them with setup like that?
Force action only works as event chain, and it can be... unreliable.
I didn't tried that, but what about "force action" script call and "post damage" notetag? Now, how would I make actor actually target the enemy which hit them with setup like that?
the state tags should have a reference for the attacker included. so you could use the force action call on that reference.

But as already stated. It wont always work 100% as intended. interrupting the turn order using it without careful consideration beforehand usually brings unintended consequences. the major problem In my project was that it consumed the actions battlers were meant to take at the start of the turn if the action was forced before they took their intended action. It might not be an issue with FTB though
the state tags should have a reference for the attacker included. so you could use the force action call on that reference.

But as already stated. It wont always work 100% as intended. interrupting the turn order using it without careful consideration beforehand usually brings unintended consequences. the major problem In my project was that it consumed the actions battlers were meant to take at the start of the turn if the action was forced before they took their intended action. It might not be an issue with FTB though
I'd still try it out, because I really want skill dependent counter. And since I'm currently almost done with first enemy fight, I want to give counter another chance.
So. about reference for attacker. I'm not sure if it can be specified, but what about choosing random target after getting hit in general?
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)
EDIT 2: I found this notetag on BattleCore page, and, well, maybe I could make enemies attacks pass some kind of variable to each other, and make it attack enemies with that variable, but honestly I have no idea how that would work with state targetting, and if it would work at all. If someone is interested, though, I'd like to try it out.

1702010050469.png
1702010718912.png
So. about reference for attacker. I'm not sure if it can be specified
the references are there specifically for you to be able to specify them. the note tags provided by the plugin that you use to to run code on hit usually gives you references to the attacker and the defender through user and target.
Im not checking because i have no need to, but it should be right there in the plugin's help file and on the yanfly website for either visustella battle core or buffs & states. Itll help you in the long run if you were familiar with their contents. its just a matter of calling the forceAction method on the defender to use whatever skill id on the attacker when hit.

what about choosing random target after getting hit in general?
using -1 as your target index randomizes the target
You'll need Yanfly's Auto Skill Triggers and the Battle Core for it to work.
I think you mean VisuStella's 😉

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