MV Prevent counterattack when woken from sleep status

Version: MV
Plugins: Yanfly Counter Control, Buff States Core, & Skill Core

Issue: Sleep in my game sets Counter Rate to 0%. When a character is put into sleep with an attack, they do not counter. This is good. However, when Sleep state is removed due to an attack, either through 'remove by damage' or through <Custom React/Respond/Deselect Effect>, the character performs a counterattack, as these options remove the state before the counterattack phase (which seems to occur after everything else, even <After Eval> in the skill notebox).

Goal: I am looking for the ideal way to prevent these counterattacks from occurring when a character who has asleep status is struck.

Is there any known method to remove a state from a defender after the counterattack phase? As mentioned above, even <After Eval> in a skill notebox will still remove the state before the counterattack phase.

I was considering having the sleep state apply a secondary state that replaces the default counter with a blank skill that removes itself, however this leads to the opposite problem more or less, where the state would remain if the character is attacked by a non-counterattack inducing skill. So preferably there is another solution to this, one that doesn't require piggybacking off of a counterattack to work.

I was surprised to see this topic not brought up before. Hopefully there is a simple enough solution here.
This only happens because of how YEP Counter Control reworks counter attacks:

Once the skill is selected, the counter skill is placed in the counter queue and waits for the current attacker's turn to be over. Once over, the actions in the counter queue will begin. The counterattacker will perform counter actions without conflicting with their own turns. This process will repeat itself until the counter queue is emptied.
Without plugins, a counter attack effectively replaces the incoming action.

You might be able to work around it by manually removing the queued counter attacks with a notetag from YEP Buffs & States Core on your Sleep state? E.g. (completely untested):

<Custom Remove Effect> var Q = BattleManager._counterQueue; for (var n = Q.length; n--;) { if (Q[n].subject() === user) { Q.splice(n, 1); } } </Custom Remove Effect>
Thank you, that does seem like it would be a working solution, however I am using several plugins that modify Counter Control and the default battle system, so unfortunately this does not work for me when tested. It does give me an idea and I bet if I find the equivalent script that replaces the default queue order, I could modify this a bit to get it working. The assistance is appreciated.
I was surprised to see this topic not brought up before.
Even if someone else were using the correct combination of plugins and states as you are to make this happen, I don't think most people would care. After all, the target is now awake, no longer sleeping, so it makes sense to me that their counter attack would work.

Without getting into any further coding of plugin extension stuff, you could do this...make your skills that inflict Sleep also inflict a second, invisible state.

That state has a Trait which seals any counter attack skills.

Then give it the Yanfly Buffs & States notetag:
Code:
<Custom Turn End Effect>
if (!user.isStateAffected(42))
    user.removeState(stateId);
</Custom Turn End Effect>

Replace 42 with the ID of your Sleep state.

That will make it so the battler has to actually get a turn where they weren't sleeping before they can counterattack.

If you want some different timing, you'll need to give a more precise explanation of how you have your counter attacking set up and under exactly what circumstances you want it to be possible.
Hey @ATT_Turan, thank you for the pointers. Pretty sure the issue I am experiencing would happen regardless of other plugins, as this seems like the logical workflow for Counter Control.

Anyways, your solution seems similar to one that I was considering, however having to wait an entire turn until being able to counterattack feels even worse than the initial wake->counter situation.

I will likely just leave it as is as you mentioned it isn't too counterintuitive, or perhaps rather opt for script that updates every turn for all actors, and to have this script disable the invisible state if the sleep state is not also present. I think I would simply need to save the sleeping battler into a variable, and run something like this every turn start:

JavaScript:
var sleepEntity = v[124]

if (!sleepEntity.isStateAffected(18) {
sleepEntity.removeState(17);}

But this comes with its own issue, in that if there are multiple sleeping battlers at the same time, they would each need their own variable. So perhaps instead of a variable, creating a function attached to each battler would likely be the way to go, but I am still fairly naive when it comes to most things outside of variables and conditions, so I am not so confident in this regard.
perhaps rather opt for script that updates every turn for all actors....if there are multiple sleeping battlers at the same time, they would each need their own variable
lets say you continue that route. at that point, it would be easier to make your variable be an array, push() sleeping battlers into it, then loop through the array and run removeState() on every element when youre ready to wake them.

So perhaps instead of a variable, creating a function attached to each battler would likely be the way to go
you could do that. theres nothing wrong with doing so, but if the function is to just wake the battler if they are asleep, it would be just as easy to use removeState() directly
Well, you still didn't do what I suggested and show how you've set up the counter attacks.

For example, if you're using the conditions from Yanfly's Counter Control, you should simply be able to include not having the sleep state as one of the conditions. I haven't looked at the code, but I assume those conditions are checked at the time of the hit.
@ATT_Turan The sleep state is removed before the counterattack as mentioned in the first post, so that will not work unfortunately.
-

Thank you @Robro33
I actually have a few states that suffer from this problem. If it isn't too much, how would I save an entity in an array instead of a variable, and then use that array to determine a subsequent action within a state?

This isn't pertaining to the sleep state, but I think the same concept would work regarding using an array instead of a variable.

For example, I have this in a skill which saves the attackers ID for further processing

<After Eval>
$gameVariables.setValue(124, a);
</After Eval>


and then the inflicted state the skill causes has the following:

<Custom Turn Start Effect>
var afflictedEntity = v[124]
afflictedEntity.gainHp(-999);
</Custom Turn Start Effect>

Ideally my goal here is to reuse this code nomatter if the state is applied to multiple entities. But currently every actor who uses the skill will take over the v[124] slot.
Then why not use the Custom Counter Skills notetag?

Instead of giving the actors any innate counter skill, you do
Code:
<Custom Counter Skills>
if (!user.isStateAffected(42))
    skills.push(69);
</Custom Counter Skills>

Where 42 is the ID of your Sleep state and 69 is the ID of your counter skill.

That gets checked before the hit is resolved, I just looked it up in the plugin.
Then why not use the Custom Counter Skills notetag?

Instead of giving the actors any innate counter skill, you do
Code:
<Custom Counter Skills>
if (!user.isStateAffected(42))
    skills.push(69);
</Custom Counter Skills>

Where 42 is the ID of your Sleep state and 69 is the ID of your counter skill.

That gets checked before the hit is resolved, I just looked it up in the plugin.

Must be the other plugins I am using that seems to break some functions. Custom Counter Skills just locks out the counter completely, and trying the equivalent code for the modified version I am using on the skill itself still suffers from the same problem where sleep is removed before the condition is checked.

I will probably just end up making a silent counter skill as the default counter that removes the silent state that activates it as its action, and then run the condition at every actor's turn start to remove this silent state if sleep is also not present. Will need to make an array for this to save afflicted members and run the check on them at every turn, so it appears I have some reading and experimenting to do.
and then the inflicted state the skill causes has the following:

<Custom Turn Start Effect>
var afflictedEntity = v[124]
afflictedEntity.gainHp(-999);
</Custom Turn Start Effect>
im not entirely sure what youre going for here with the info given. if this (self inflicted?) state is already on the affected battler, then you dont need the variable to hold them. within that note tag, you can access the battler the state is on with either "user" or "target", the battler that applied the state with "origin", and your code would run on all the affected battlers independent of eachother.

but to answer the question about the array with an example...

Spoiler
youd have your skill start by setting up an array to hold the battlers in, push the battlers in as the the skill is used, and then set the game variable's value to be the array

Code:
<After Eval>
var list = $gameVariables.value(124) || [];  
list.push(a);
$gameVariables.setValue(124, list);
</After Eval>

now that that variable is an array, you can then use array functions like forEach to do something to every member of the array like this
Code:
<Custom Turn Start Effect>
$gameVariables.value(124).forEach(battler => battler.gainHp(-999));
</Custom Turn Start Effect>
and then take the time at some point to clear that variable's value so that it isnt just accumulating battlers

But as i mentioned, that tag's code will be ran once for every battler that has that state active at the start of the turn independent of the others. if the skill is used three times, that array would have 3 battlers in it. each battler would have the state, and each instance of the state would damage all of the battlers in the array. you could remove the battlers from the array as they get damaged, but at that point its questionable as to why you'd allow multiple of the state to be active if only one instance of it would ever do anything.

using "user" would be much more straightforward. you'd just dump the after eval tag on the skill and give the state this:
Code:
<Custom Turn Start Effect>
user.gainHp(-999);
</Custom Turn Start Effect>
Thank you @Robro33,

I actually left some things out to try and simplify it but that probably wasn't the best idea. Do you mind if I PM you? It is a leechseed type state I am working on, which requires saving the attacker's ID and giving back HP during the defender's turn start or end.

The info you posted nevertheless is extremely helpful and I think I can use that to fix the sleep->counter issue I was working on.
yeah, go for it

Original thread: https://forums.rpgmakerweb.com/threads/prevent-counterattack-when-woken-from-sleep-status.168122/