MV Yanfly Buff and States - State that can only be applied X times.

Hello! 👋

Could someone help me with creating a state that can only be applied to a target X times, before they become immune? The idea being that instead of making a boss immune to powerful status effects like Stun, I can instead make it so an enemy can only be stunned up to X times within a battle.

One of Yanfly's Tips and Tricks is pretty close to what I'm looking to do, their Cyclone state.

As an added bonus, if it is possible, could this counter affect multiple different "stun-like" states. For example, I have another state called Stop. If the limit of X stuns per enemy could account for both states, that would be awesome, but not required if it would be too difficult.

Thank you!
do you want to record it in one battle only?

then reserve eight variables for the possible eight enemies in a battle.
use javascript code on state condition to check what the variable of the corresponding index is - if the result is higher the condition fails and the state can't be applied.
then use javascript code on the execution tag to count up the corresponding variable if the state is applied.
don't forget to null the variables at the beginning of each combat (troop event on turn 0, possibly through base troop event plugin)

Sorry, I can't give you the notetag code itself, but the sequences above should work if you use the correct code.

If you want to store the number of stuns beyond a single battle, you'll need self-variables and things become much trieckier.
One of Yanfly's Tips and Tricks is pretty close to what I'm looking to do, their Cyclone state.
That's exactly what you want to do, you just need to set them as resistant to the state when the counter reaches whatever number you want. Simply make another state that has the trait of resistance to your stun state, and put in your original stun state:
Code:
<Custom Remove Effect>
// Use whatever variable name you added to the battler in your Custom Apply Effect
// and whatever you want the number of times they can be successfully stunned
if (target._stunTimes == 4)
    target.addState(X);
// Where X is the ID of your state with stun resistance
</Custom Remove Effect>
As an added bonus, if it is possible, could this counter affect multiple different "stun-like" states.
You add whatever states you want it to be resistant to in its traits.
Thank you very much for both of your help! 🙂

I ultimately went with ATT_Turan method, with a few minor changes to the Cyclone notetags to get things working with several different stunning states, without needing several of the "bridge" states.

In case anyone sees this and wants to recreate it for yourself here's my setup.

You'll need 2 new states, in addition to however many states you want to have add to the counter limit.

Spoiler: Stun Statuses
Add this notetag to all of the states you want the Stun limit to apply to.
In this case, 119 is the Stun Resistance State, and 120 is the Bridge State
Code:
<Custom Apply Effect>
target.addState(120);
</Custom Apply Effect>

<Custom Remove Effect>
// Use whatever variable name you added to the battler in your Custom Apply Effect
// and whatever you want the number of times they can be successfully stunned
if (target._stunTimes == 3)
    target.addState(119);
// Where X is the ID of your state with stun resistance
</Custom Remove Effect>

Spoiler: Bridge State
Make sure the Bridge State only wears off at the end of battle. Otherwise, this state is completely empty besides this note tag.
Code:
<Custom Apply Effect>
// Set the target's default stun times to 0.
target._stunTimes = target._stunTimes || 0;
// Raise the number of times the target has been affected by Stun.
target._stunTimes += 1;
</Custom Apply Effect>

<Custom Remove Effect>
// Clear the number of times the target has been affected by Stun.
target._stunTimes = undefined;
</Custom Remove Effect>

Spoiler: Stun Resistance State
This state is simply set to make the user resistant to all of the stun states.
Set it only to be removed at the end of battle.
There's no reason for you to use that bridge state, unless you're also using this on your actors (which you didn't say before, you only mentioned bosses).

If you're only using this on enemies, simply put that apply effect directly into the stun state.
Oh yeah, I forgot that I wouldn't actually need to reset the stunTimes variable for enemies. Thanks for the heads up!

Original thread: https://forums.rpgmakerweb.com/threads/yanfly-buff-and-states-state-that-can-only-be-applied-x-times.141981/