Force an action when a battler has full TP

Okay, so here's what I'm trying to do. In my RPG, when someone (either an actor or an enemy) gets to max TP, they instantly (disregarding turn order) and automatically trigger a powerful special move. I want different characters and enemies to have different specials with different effects, but at its most basic, its a high damaging finisher. The move drains all of the character's TP and they enter a Weakened state, then the regular turn order resumes.

So I guess what I need is a script that activates a Forced Action when a battler has 100 TP, the parameters of which I can manipulate with Visustella's BattleCore.

////

Here's my attempt. I based it on the Trance state from the Tips & Tricks thread.

JavaScript:
JS Passive Condition>
if ($gameParty.inBattle()) {
user._trance ??= false;
condition = user._trance || user.tp === user.maxTp();
} else {
condition = false;
}
if (condition) {
if (!user._trance) {
$gameTemp.requestAnimation([user], 120);
user.forceAction(26, a.index);
BattleManager.forceAction(user);
this.setWaitMode('action');
user._trance = false;

}

}


</JS Passive Condition>

<JS Pre-Regenerate>

if (user.tp === 0) {
user._trance = false;

}
</JS Pre-Regenerate>

And it worked! ...almost sorta. But there were a few major issues with the code.

1) First of all, the animation activates like a 100 times, creating this supernova-like effect that looks cool, but is obviously busted.
2) I have trouble getting rid of the "Trance" state. Enemies will never lose their their Trance state and attack continuously. My character will use the attack properly once, but every subsequent Trance will just endlessly loop until all enemies are dead.
3) Even if it worked properly, this script only ever triggers one action: Action26. I want to be able to use different actions depending on who gets max TP.


This mechanic is at the core of my battle system, so if anyone can help me get this to working I'd appreciate it.
user._trance ??= false;
condition = user._trance || user.tp === user.maxTp();
} else {
condition = false;
}
I don't see why you need all that. It's quite a few code statements for what it accomplishes, and it is using two variables instead of one which, I believe, is leading to confusion in the next chunk.
if (condition) {
if (!user._trance) {
This appears to be wrong to me. Since you're succeeding on the if check when user._trance is false, why would you set it to be false again 5 lines later? And isn't the purpose of this to make them do the skill when they're in trance? I think that's why you're getting the repetition, because this condition is always evaluating to true - nothing in your code is ever making user._trance not be false.

Notice that in the original post from Trihan, the statement inside of this conditional sets user._trance to true, whereas you changed it to say false.

I would suggest you correct that line, see if it solves your problems. If not, we can refactor it a bit, trim down some of the code and see if that helps to clarify things.

Original thread: https://forums.rpgmakerweb.com/threads/force-an-action-when-a-battler-has-full-tp.149366/