(Shield)State from item that is removed after one hit?

Im stumped trying to make an item that increases defence by 50% until you take damage, once each battle (a sort of consumable armor buff).

I created the state which increases DEF and is removed by damage. Then I use Yanfly auto passive to put it on my shield, however the state will never be removed even when damaged.

Ideally I would create a magic shield with
Minor Shield of Protection- temporarily defence by 50% for 1 attack.
Shield of Protection- temporarily defence by 50% for 3 attacks.
Major Shield of Protection- temporarily defence by 50% for 5 attacks.
I can help you if you are also using Yanfly's Buffs and State Cores. Put the codes below into your state note tag:
Code:
<Custom Apply Effect>
var atkcount = 1; //This is the attack counter. Change it to fit your purpose
target._attackCount = target._attackCount || atkcount;
target.setStateCounter(x, target._attackCount); //Set a counter to your state. Change X to your state ID
</Custom Apply Effect>

<Custom React Effect>
//Check if actor takes dmg
if (value > 0 && !this.isHpRecover()){
//Decrease the attack counter by 1
target._attackCount -= 1;
//Set a counter to your state. Change X to your state ID
target.setStateCounter(x, target._attackCount);
//Check if attack counter reaches 0
if (target._attackCount <=0){
//Remove the state. Change X to your state ID
target.removeState(x);}}
</Custom React Effect>

<Custom Remove Effect>
user._attackCount = undefined;
</Custom Remove Effect>

Currently, the attack counts are not reset if you apply the state again. If you want to reset the attack counts each time the state is applied, just replace "target._attackCount = target._attackCount || atkcount;" (inside <Custom Apply Effect> with this:
Code:
target._attackCount = atkcount;
Look over here!

Awesome thanks! It works, I was doing it a very complicated way that would drive me nuts later on, this cleans it up.

Im unsure of a few things you said put in your state note, but if its an item giving the state, I should put this into the item note?

Currently, the attack counts are not reset if you apply the state again. If you want to reset the attack counts each time the state is applied, just replace "target._attackCount = target._attackCount || atkcount;" (inside <Custom Apply Effect> with this:
Code:
target._attackCount = atkcount;
I did not follow this part, it does not reset? I am making it, so that if your using Shield X, it provided the one hit of damage reduction. In theory will it not work if I had 2 different items equipped that do the same thing? It would still only take 1 hit not 2?

Look over here!

Thanks! Ill use it if I have to, trying to limit how many scripts I have since I have a lot already!
I did not follow this part, it does not reset? I am making it, so that if your using Shield X, it provided the one hit of damage reduction. In theory will it not work if I had 2 different items equipped that do the same thing? It would still only take 1 hit not 2?
Oh, I did not read the part where you told you were using this as a passive state. If you do not set conditions for the passive state, I will never be removed. In this case, it needs to be changed a bit. The state you mentioned is apart of some armor, right? I thought it was from some skills or consumables which apply the state on the actor upon use. Just give me some minutes. I will try to adjust the codes.

EDIT: Here are the new codes. The passive state only activates in battle, and if the user takes a certain number of attacks, the state is removed and cannot be reset before a new battle. Put these codes below into your state's note tag, not item (as you are using auto passive state):

Code:
<Custom Passive Condition>
if ($gameParty.inBattle() && user._guardCheck !== 1){
var atkcount = 1; //This is the attack counter. Change it to fit your purpose. 1 means it will be removed after 1 attack and so on
user._attackCount = user._attackCount || atkcount; //Set this flag to count your attacks received
user.setStateCounter(x, target._attackCount); //Set a visual counter to your state. Change X to your state ID
condition = true; //Enablethe passive state
}
else{
user._guardCheck = undefined;
user._attackCount = undefined;
condition = false; //Disable the passive state
}
</Custom Passive Condition>

<Custom React Effect>
//Check if actor takes dmg
if (value > 0 && !this.isHpRecover()){
//Decrease the attack counter by 1
target._attackCount -= 1;
//Set a counter to your state. Change X to your state ID
target.setStateCounter(x, target._attackCount);
//Check if attack counter reaches 0
if (target._attackCount <=0){
target._guardCheck = 1;}}
</Custom React Effect>

I have not tested this, but I hope it should work.
Oh, I did not read the part where you told you were using this as a passive state. If you do not set conditions for the passive state, I will never be removed. In this case, it needs to be changed a bit. The state you mentioned is apart of some armor, right? I thought it was from some skills or consumables which apply the state on the actor upon use. Just give me some minutes. I will try to adjust the codes.

EDIT: Here are the new codes. The passive state only activates in battle, and if the user takes a certain number of attacks, the state is removed and cannot be reset before a new battle. Put these codes below into your state's note tag, not item (as you are using auto passive state):

Code:
<Custom Passive Condition>
if ($gameParty.inBattle() && user._guardCheck !== 1){
var atkcount = 1; //This is the attack counter. Change it to fit your purpose. 1 means it will be removed after 1 attack and so on
user._attackCount = user._attackCount || atkcount; //Set this flag to count your attacks received
user.setStateCounter(x, target._attackCount); //Set a visual counter to your state. Change X to your state ID
condition = true; //Enablethe passive state
}
else{
user._guardCheck = undefined;
user._attackCount = undefined;
condition = false; //Disable the passive state
}
</Custom Passive Condition>

<Custom React Effect>
//Check if actor takes dmg
if (value > 0 && !this.isHpRecover()){
//Decrease the attack counter by 1
target._attackCount -= 1;
//Set a counter to your state. Change X to your state ID
target.setStateCounter(x, target._attackCount);
//Check if attack counter reaches 0
if (target._attackCount <=0){
target._guardCheck = 1;}}
</Custom React Effect>

I have not tested this, but I hope it should work.

Im a bit confused now lol, the way you first posted worked great (although I only tested it with 1 attack), I put the note in the item itself and created a state that increased DEF by 50% removed when taking damage. It worked everytime I tested it.

The new note I put in the state and for the item I just put <passive state: X> to give me the state, but it never turns off now.
Im a bit confused now lol, the way you first posted worked great (although I only tested it with 1 attack), I put the note in the item itself and created a state that increased DEF by 50% removed when taking damage. It worked everytime I tested it.

The new note I put in the state and for the item I just put <passive state: X> to give me the state, but it never turns off now.
The first post I wrote was to be used for the consumables, or skills which apply the state once, not passive. If you are not using a passive state, you can keep using the first one. And for the passive one in the second post, I think I will try to test it myself to see if something wrong.

For the first post, if you want to change to 2 or 3 attacks, just change this line in <Custom Apply Effect>
Code:
var atkcount = 2; //or any number of attacks you prefer

Original thread: https://forums.rpgmakerweb.com/threads/shield-state-from-item-that-is-removed-after-one-hit.120275/