MV Run a <Custom Remove Effect> before state expiration (barrier)

Hi, I'm looking for a way to run some code as state ends before it actually does. <Custom Remove Effect> runs after the state has gone, so it doesn't quite work.

- YEP Buff Core
- YEP Absorption Barrier
- (YEP CTB Battler)

Here's more details:

I'm trying to recreate the Haima skill from FFXIV. You get a small barrier and everytime it breaks, a new one gets automatically generated. I made this bit work through different states (to use different icons for better readability).

Here's my state 164 :

JavaScript:
//Set the barrier and stores the barrier points in the state counter, to keep the player informed.
<Custom Apply Effect>
    var barrier = $gameActors.actor(3).mat * 1
    target.gainBarrier(barrier)
    target.setStateCounter(164, barrier);
</Custom Apply Effect>


//Checks if the damage recieved is higher than the current barrier, if so the state is removed, if not updates the counter.
<Custom Respond Effect>
    if (value > 0 && this.isHpEffect() && target.hp > 0) {
        if (value >= target.getStateCounter(164)) {
            target.removeState(164);
        } else {
            target.addStateCounter(164, value * -1);
        }
    }
</Custom Respond Effect>


//If the state is expired, battler will loose the same amount of barrier points that were stored in the counter. Applies the second barrier through state 165.
<Custom Remove Effect>
battler.gainBarrier(target.getStateCounter(164) * -1)
    target.startAnimation(128, true, 1);
    target.addState(165);
</Custom Remove Effect>

Sadly that last part doesn't work. I've managed to sneak my way outside of this problem by having different barriers not stackable, the state removes every barrier point the battler may have on applying and removing. I'll keep it that way for other characters as the game has been balanced with that in mind, but I'd like this one character to be able to stack both of his small barriers...

I've tried using barrier turns, but they somehow end up not matching the state turn count...

I thought if I could just get that state counter as it runs out, I could make it work (putting a condition on the other skill in case both of them are active so damage don't get taken out of both counters).

Maybe the solution is somewhere else altogether and I need a new perspective, if any of you have any idea I'd be glad :'(
For one thing...are you sure it doesn't work? I haven't looked at the plugin code to confirm whether this will run before or after the state is actually deleted from the actor. But you have the wrong code in your Remove effect. You're saying battler.gainBarrier() instead of target.gainBarrier()

"battler" is just a generic word saying you can reference any battler there, actor or enemy. It's not an actual variable the notetag will recognize.

If that change fixes it for you, awesome! If not:

One of the techniques you'll see in a lot of Yanfly's Tips & Tricks is making variables stored on the actor itself.

Because that has nothing to do with the state, such a variable can be referenced even after the state is gone.

It's up to you whether this replaces the state counters or is used along with them (I don't know whether the counters are visible for player info or just for you to track).

But, for example, in your first notetag you'd do something like:
Code:
    target.setStateCounter(164, barrier);
    target.barrierCounter=barrier;
Then in the Respond:
Code:
            target.addStateCounter(164, value * -1);
            target.barrierCounter-=value;

And in the Remove:
Code:
target.gainBarrier(target.barrierCounter * -1)
Thanks for the quick reply!

For one thing...are you sure it doesn't work? I haven't looked at the plugin code to confirm whether this will run before or after the state is actually deleted from the actor.

It says it does run after in the help file, but I did check to make sure by just storing the counter into a variable at that moment and popping a text window, it would always return 0.
Code:
http://www.yanfly.moe/wiki/Buffs_%26_States_Core_(YEP)
  The code in between these notetags will [...] process after the state is actually removed but before <Custom Leave Effect>.

But you have the wrong code in your Remove effect. You're saying battler.gainBarrier() instead of target.gainBarrier()
Oops, a copy/paste mistake, I should know better

One of the techniques you'll see in a lot of Yanfly's Tips & Tricks is making variables stored on the actor itself.
Oh that's brilliant! I didn't know that was a thing! Just tested it I'm shocked how well it works. Now comes the issue of the respond effect taking place on both states, but it should be fairly easy to fix 🙂 It opens up so much possibility and that's going to clear up my variable list a bit in the future. Thank you so much, thread solved 😀

Original thread: https://forums.rpgmakerweb.com/threads/run-a-custom-remove-effect-before-state-expiration-barrier.157431/