Counterattack after receiving damage (W/O Scripts or Plugins)

Hello, first of all, I'm sorry if a thread like this already exist and I'm unaware of it...

I'm trying to make a skill that acts like a counterattack, but it happens after the user got hit by an enemy attack. To simplify, the fight will go:

-) User uses a skill that allows them to counterattack
-) Enemy attacks User
-) User receives damage from enemy attack
-) User will immediately counterattack the enemy that attacks them

To this, I have made the following:

-) Created a 'Shield' skill to give the user a 'Shielded' state.
-) Created a battle event with the conditional branch "If user state = 'Shielded', then it'll force action user to use 'Counter' at the enemy that attacks him the most recent.
-) Set the battle event's span to 'moment' so the 'Counter' skill will be used each time an enemy attacks the user.
-) I've made a second battle event that forces the user to use the 'Shield' spell at the beginning of the battle, at turn 0

But, things really don't go well. Instead of getting what I want, these happened instead. With the 'Don't Run' condition in the first battle event, it never worked. I tried to set the condition 'If user have less then 99% HP', but what I got is the user using the 'Counter' skill ENDLESSLY on the enemy at the first index, even though I choose 'last target' at the force action (The enemy at the second index is the first one that damages the user)

I guess my first question is, what is the optimal trigger I should put in the first battle event? Second, how do you make the user only use the skill only once? And third, how do you make sure that the 'Counter' skill get used on the enemy that attacks the user most recently?

I'm hoping I can do it without having to resort to scripts or plugins, since I have ZERO experience with it and have no idea how to use them.

Thank you for reading my wall of text, I hope someone can help me~
Afraid this requires a plugin as the default counterattack attacks before you are hit and your hit never hits so you'll need to override them.

Now if you dont use cnt for counterattack then there might be a workaround but if you do use cnt for it then a script is needed
I didn't make this, but i found it somewhere. The second counter type doesn't work, but the third is what your looking for and works

Spoiler
# 1224Deko - Hamedo miniscript v1.00
#
# This script overrides Scene_Battle and def invoke_item in it.
# No credits needed.
# Inspired from this thread:
# http://www.rpgmakervxace.net/topic/16995-taking-damage-when-counterattacking/
# So it would be nice if you credit xShinryu and P Bevans.
#
# Setup:
# Each State must have Ex. Param. CNT +100% in Features to Working.
# Exchange ID States in module Hamedo to your owns.
#
# Add 3 ways of counter-attack mechanism
#
# Evade and Counter: Default mechanism. Probably no-needed. Add only for priority.
# Hamedo: Character Deals damage before taking damage.
# Counter Attack: Both deals damage.
#
# If you use only another ID state with CNT ratio, will be working like default mechanism.


$imported = {} if $imported.nil?
$imported["HamedoState"] = true


module Hamedo

# Setup Evade and Counter state ID
EAC = 74

# Setup Hamedo state ID
HAMEDO_STATE = 75

# Setup Counter Attack State ID - Damage taken before Countering
CNT_ATK = 73

end

class Scene_Battle < Scene_Base
def invoke_item(target, item)
# Evade and Counter damage
if rand < target.item_cnt(@subject, item) && target.state?(Hamedo::EAC)
# apply_item_effects(apply_substitute(target, item), item)
if target.hp > 0
invoke_counter_attack(target, item)
end
# Hamedo Status
elsif rand < target.item_cnt(@subject, item) && target.state?(Hamedo::HAMEDO_STATE)
# apply_item_effects(apply_substitute(target, item), item)
if target.hp > 0
invoke_counter_attack(target, item)
end
if subject.hp > 0
apply_item_effects(apply_substitute(target, item), item)
end
# Counter Attack section
elsif rand < target.item_cnt(@subject, item) && target.state?(Hamedo::CNT_ATK)
apply_item_effects(apply_substitute(target, item), item)
if target.hp > 0
invoke_counter_attack(target, item)
end
# Original Counter-Attack Mechanism
# Original invoke_item
elsif rand < target.item_cnt(@subject, item)
invoke_counter_attack(target, item)
elsif rand < target.item_mrf(@subject, item)
invoke_magic_reflection(target, item)
else
apply_item_effects(apply_substitute(target, item), item)
end
@subject.last_target_index = target.index
end
end
Now if you dont use cnt for counterattack then there might be a workaround but if you do use cnt for it then a script is needed

I do plan to use the battle event to work around the CNT, but so far no luck. I made the 'Counterattack' skill as a separate skill.

Though, might be off topic, but is there a 'how to use plugins for dummies?'. As much as I want to use plugins, I don't know how to 'activate' it 😳
If you want to not use plugins, thats an easy one. Create a dummy state where all it does is wear off after a time, nothing else is needed in the state except an icon. Next, have a common event attached to all the enemy attack skills that checks if that charecter has the state. if they do, force action attack. If you want it to only happen after the charecter is attacked. You'll want to set a variable to the charecters HP, and have it check if it's less. I reccomend attaching a common event to the charecters skills and make damage over time effects happen when there making an action, but before they make it. And boom! Counter after hit with only events
If you want to not use plugins, thats an easy one. Create a dummy state where all it does is wear off after a time, nothing else is needed in the state except an icon. Next, have a common event attached to all the enemy attack skills that checks if that charecter has the state. if they do, force action attack. If you want it to only happen after the charecter is attacked. You'll want to set a variable to the charecters HP, and have it check if it's less. I reccomend attaching a common event to the charecters skills and make damage over time effects happen when there making an action, but before they make it. And boom! Counter after hit with only events

Ok, I've tried something to that effect So, what I did were:
-) Created two variables. The first is 'HP check', set to a constant with the same number as the character's max HP. The second one is the character's HP itself set via the game data. Both are controlled in the Battle event in the Troops section of the database, and is done in Turn 0
-) Created the 'dummy' state, which will wear off after a set of turns. Note that it will be inflicted to the character if they use the 'Shield' skill.
-) Created a common event, where I put two conditional branches. If 'HP check' is greater or equal to the character's HP, and if the character is inflicted with the 'dummy' state, it will be forced to use the 'Counterattack'. Trigger is currently set to 'None'

When I test the battle, the character will use the 'Shield' spell on turn 0, but will immediately use the 'Counterattack' on Index 1. After that, they never use the 'Counterattack' anymore, only that one time. I guess it solves the endless looping counterattack~

My next batch of questions would be, do I have to set the common event's trigger? If so, what is it, and what is the condition switch? And what do you mean by "attaching a common event to the charecters skills and make damage over time effects happen when there making an action, but before they make it."?

Thank you for bearing with me, I'm such a beginner 😕
Connect the common event to enemies damaging skills

Have the common event check if the state is applied through a conditinal branch

If it is applied, have it check if current HP is LOWER than previous HP

If it is lower, have the common event connected to the enemies skill have the charecter with the state use "attack"

Connect another common event to the charecters skills that sets the HP

Make all damage over time effects happen at the beginning of the actors turn. So they don't trigger the counter if the actor isn't attacked
@Oddball : How would that though work in context of the default behavior, which is if the CNT roll is made, the one being attacked attacks the attacker, attacker never gets a turn. Even force action will still run into the default behavior, and might result in an odd combo of CNT chains.
The common events won't connect to the regular attack. Am i the only one that gives enemies there own attacks and never give them "attack"?
Connect the common event to enemies damaging skills

Have the common event check if the state is applied through a conditinal branch

If it is applied, have it check if current HP is LOWER than previous HP

If it is lower, have the common event connected to the enemies skill have the charecter with the state use "attack"

Connect another common event to the charecters skills that sets the HP

Make all damage over time effects happen at the beginning of the actors turn. So they don't trigger the counter if the actor isn't attacked

I already make the 'Counterattack' skill (The one that damages whoever attacks the character) call the common event. Do I need to make the 'Shield' spell (The one that gives the character the 'counter' state) the same?

By previous HP, do you mean Max HP, or do I have to make more variables to sub the HP? (If so, I'm not sure how). Also, I don't think it effects anything, but I check if the character have the 'counter' state before I check the HP.

I might need an example for the last two steps, as I'm not sure how to do it :v
The issue is force action attack still goes through the counterattack loop, as ANY physical skill goes through that loop, not just the default attack. Plus I'm not sure if common events even run if you trigger the counterattack loop.
It worked for me. Here. I'll give a simple example of what i'm talking about. It doesn't loop for me, sense i don't connect the common event to "Attack"

Edit: Once you get this part working, i'll go through the next steps

Spoiler
counter common event.png
Counter enemy.png
Counter skill.png
That's because you use magical attack. If you want to use physical attack you can't avoid it. But you do need physical attack if you want to use evade and to hit, unless you want to manually calculate all of those too.

BTW, by using magical attack, you can be reflected via the magic reflection state. Give someone 100% magic reflection and see what happens.
I just made Acid spray a physical attack and tested it. It still works. This method is ignoring the CNT parameter completley

Edit: the Gaurd2 state is empty
That is impossible with the code, sorry. The code directly runs when physical attack is set, so you have to have it disabled somehow.

Edit: Here is the default code for how CNT is counted:

def item_cnt(user, item)
return 0 unless item.physical? # Hit type is not physical
return 0 unless opposite?(user) # No counterattack on allies
return cnt # Return counterattack rate
end

As you can see it clearly returns it on physical attack. Also skills are treated as items in the database.

And here's the code for when you invoke anything in the game (skills or spells)

def invoke_item(target, item)
if rand < target.item_cnt(@subject, item)
invoke_counter_attack(target, item) #This is the key line that is not avoided on physical attacks, unless you change how CNT works or is computed.
elsif rand < target.item_mrf(@subject, item)
invoke_magic_reflection(target, item)
else
apply_item_effects(apply_substitute(target, item), item)
end
@subject.last_target_index = target.index
end

That uses that result from the previous code to decide what to do. Force action just adds the skill into the system and executes it as a skill though so it won't ignore all of this, unless you already changed the default code some.

FYI: I myself use force action to simulate a boss acting 2x so as to give me better control on what it uses on action 2, and have both counterattacked them, reflected and dodged that 2nd attack so it isn't ignoring the code, at least unless you changed it somehow.
Ugh. This is what i mean

As you can see. CNT is not even being used. I'm suggesting he ignore the CNT parameter completley and just do it with eventing

Guard2.png
Now that makes sense. See what I was saying is IF you want to use CNT then the code will have problems (I believe I said that a few posts ago somewhere). If you don't then your method will work perfectly.

Same with magical reflect though too, FYI.
Okay, I followed what you showed me so far, and the attack worked at least. The character immediately uses it once since I created a battle event that gives them the 'empty state'.

I have to say it again, thank you for being so patient with me 🙂

Edit: To avoid some confusion that seems to be going on, I'm not using the CNT state.

Original thread: https://forums.rpgmakerweb.com/threads/counterattack-after-receiving-damage-w-o-scripts-or-plugins.114564/