MV Anyway to check if actual action in battle was somehow forced?

I'm trying to create a passive state in which a specific class may, randomly, attack during other allies turn. In my first post I asked for a rogue, but now I'm thinking it would suit better my monk and it's evolution-classes. The monk has 5 sequential multiattack skills. Each one, when used, apply a dummy state, which allows him to use the next stronger one. So I had the following code, presenting two issues:

JavaScript:
<Custom Establish Effect>
if (this.isForOpponent()) {
    const monks = user.friendsUnit().aliveMembers().filter(member => member !== user && (member._classId === 17 || member._classId === 18 || member._classId === 19));
    monks.forEach(monk => {
    if (monk.canMove()) {
        const bonusChance = monk.agi; // For testing purposes
        const random = Math.random();
        if (random < bonusChance) {
            var action = 1;
            if (monk.isStateAffected(249)) {action = 388; monk.removeState(249); monk.addState(250);}
            else if (monk.isStateAffected(250)) {action = 389; monk.removeState(250); monk.addState(251);}
            else if (monk.isStateAffected(251)) {action = 390; monk.removeState(251); monk.addState(252);}
            else if (monk.isStateAffected(252)) {action = 391; monk.removeState(252);}
            monk.forceAction(action, -2);
            BattleManager.forceAction(monk);
        }
      }
    });
}
</Custom Establish Effect>

The first issue is that, in a party full of monks, they may be able to keep triggering the other indefinitely. The second issue is that, using Yanfly's Action Sequences, every skill with multiple "action effect"s have multiple chances to trigger the monks' attacks - and when it triggers in the middle of the effects, it glitches the whole action sequence. I have tried tweaking these and changing from Establish Effect to Conclude or Action End, but somehow it made it all even more confusing, and the states were no longer being added/removed from the attacking monk, but from the first one who triggered the attacks.

So I thought maybe, if I could somehow check in line 2 if the triggering attack was forced, it would solve at least part of my problems. Has anyone heard of something like that? 😅
There is a property of Game_Action called _forcing that is set when the action is created (either by normal commands, enemy AI determination, or a Force Action command), which holds whether the action was the battler's decision (false) or was a Forced Action (true). So you simply need to check whether that property is true. See below for a few examples of _forciing being set, and then checked/used, within RMMV's code base.

However, you do need to know to refer to the instance of Game_Action that is currently being executed, within a Yanfly notetag, as I don't think this._forcing will work in the context of a Yanfly notetag since "this" no longer refers to the Action. And that part I don't remember how to do, although I think it's doable and not even that hard. Maybe someone else can help you out (and remind me!).

Spoiler: Examples in Code Base
JavaScript:
Game_Action.prototype.initialize = function(subject, forcing) {
    this._subjectActorId = 0;
    this._subjectEnemyIndex = -1;
    this._forcing = forcing || false;
    this.setSubject(subject);
    this.clear();
};

JavaScript:
Game_Action.prototype.isValid = function() {
    return (this._forcing && this.item()) || this.subject().canUse(this.item());
};

JavaScript:
Game_Action.prototype.makeTargets = function() {
    var targets = [];
    if (!this._forcing && this.subject().isConfused()) {
        targets = [this.confusionTarget()];
    } else if (this.isForOpponent()) {
        targets = this.targetsForOpponents();
    } else if (this.isForFriend()) {
        targets = this.targetsForFriends();
    }
    return this.repeatTargets(targets);
};
There is a property of Game_Action called _forcing that is set when the action is created (either by normal commands, enemy AI determination, or a Force Action command), which holds whether the action was the battler's decision (false) or was a Forced Action (true). So you simply need to check whether that property is true. See below for a few examples of _forciing being set, and then checked/used, within RMMV's code base.

However, you do need to know to refer to the instance of Game_Action that is currently being executed, within a Yanfly notetag, as I don't think this._forcing will work in the context of a Yanfly notetag since "this" no longer refers to the Action. And that part I don't remember how to do, although I think it's doable and not even that hard. Maybe someone else can help you out (and remind me!).

Spoiler: Examples in Code Base
JavaScript:
Game_Action.prototype.initialize = function(subject, forcing) {
    this._subjectActorId = 0;
    this._subjectEnemyIndex = -1;
    this._forcing = forcing || false;
    this.setSubject(subject);
    this.clear();
};

JavaScript:
Game_Action.prototype.isValid = function() {
    return (this._forcing && this.item()) || this.subject().canUse(this.item());
};

JavaScript:
Game_Action.prototype.makeTargets = function() {
    var targets = [];
    if (!this._forcing && this.subject().isConfused()) {
        targets = [this.confusionTarget()];
    } else if (this.isForOpponent()) {
        targets = this.targetsForOpponents();
    } else if (this.isForFriend()) {
        targets = this.targetsForFriends();
    }
    return this.repeatTargets(targets);
};

Thank you very much! It worked fine with a "&& !this._forcing" in line 2, and now my triggered attacks don't trigger attacks. I still have the action sequence problem though. My first monk gives a two-punch-attack and not only he's interrupted right after the first punch, he apparently gets mad and punches FOUR times - and I have absolutely no idea of where these are coming from. Would you suggest that I open a new thread to talk about it?
Thank you very much! It worked fine with a "&& !this._forcing" in line 2, and now my triggered attacks don't trigger attacks. I still have the action sequence problem though. My first monk gives a two-punch-attack and not only he's interrupted right after the first punch, he apparently gets mad and punches FOUR times - and I have absolutely no idea of where these are coming from. Would you suggest that I open a new thread to talk about it?
Oh! I should have noticed (from the "isForOpponent" call) that "this" does refer to the current Action being executed, which makes the use of _forcing very easy in this context! 🙂 Glad it worked well for you.

My guess for why the interruption after the first punch is happening (and perhaps the four punches total) is that the scripting is probably run after each hit rather than after the entire action completes. You might want to use a Conclude effect rather than an Establish effect? If that doesn't work... I don't know. This was always happening; it didn't only happen once you added the _forcing condition, right? If that's right, then yeah, I would suggest opening a new thread to ask.
Okay, I kinda fixed it, but, for some reason, it doesn't work every time :kaosalute:

JavaScript:
<Custom Establish Effect>
if (this.isForOpponent() && !this._forcing) {
    const monks = user.friendsUnit().aliveMembers().filter(member => member !== user && (member._classId === 17 || member._classId === 18 || member._classId === 19));
    monks.forEach(monk => {
    if (monk.canMove()) {
        const bonusChance = monk.agi / 1000;
        const random = Math.random();
        const monksQuantity = ((monks.length - 1) / 60)
        if (random < bonusChance - monksQuantity) {
            monk._isTriggered = true
        }
      };
    });
}
</Custom Establish Effect>

<Custom Action End Effect>
    const monks = user.friendsUnit().aliveMembers().filter(member => member !== user && (member._classId === 17 || member._classId === 18 || member._classId === 19));
    monks.forEach(monk => {
        if (monk._isTriggered) {
            if (monk.isStateAffected(252)) {monk.forceAction(391, -2); BattleManager.forceAction(monk); }
            else if (monk.isStateAffected(251)) {monk.forceAction(390, -2); BattleManager.forceAction(monk);}
            else if (monk.isStateAffected(250)) {monk.forceAction(389, -2); BattleManager.forceAction(monk);}
            else if (monk.isStateAffected(249)) {monk.forceAction(388, -2); BattleManager.forceAction(monk);}
            else {monk.forceAction(392, -2); BattleManager.forceAction(monk);}
            monk._isTriggered = undefined;
        }});
</Custom Action End Effect>

Sometimes a monk gets triggered (I tested by playing an animation for triggered monks) but doesn't attack, and whenever more than one monk get triggered, no more than one monk attacks (sometimes one does, sometimes none does). I'm guessing there's some incompatibility between the Force Action thingy and the ATB System I'm using 😐
:kaosalute:
Sometimes a monk gets triggered (I tested by playing an animation for triggered monks) but doesn't attack, and whenever more than one monk get triggered, no more than one monk attacks (sometimes one does, sometimes none does). I'm guessing there's some incompatibility between the Force Action thingy and the ATB System I'm using 😐
That could certainly be the case. But also check to make sure that your Dummy States aren't being removed at the wrong time.

Original thread: https://forums.rpgmakerweb.com/threads/anyway-to-check-if-actual-action-in-battle-was-somehow-forced.137867/