Help with a 'Wild Mage' style mechanic

In my game, one of the characters is a mage partially possessed by a chaotic entity, which causes their magic to sometimes go wild and have chaotic effects, similar to the wild mage subclass from DnD. I'm having trouble figuring out how to go about forcing the character to use a different skill when their magic goes chaotic. The way I'm trying to structure it is:
Code:
if stype of Skill about to be used == 2 (Black Magic) {
    let randomNumA = number between 1 and 20
    
    if randomNumA <= Total stacks of User's Passive State {
        let randomNumB = number between 24 and 33 (skill IDs of 10 random effects)
        Force use of Skill at randomNumB
        Cancel use of originally selected skill
        Add 1 stack to Passive if not at MAX stacks (MAX = 10)
    }
}

I've tried to achieve this by using a switch to act as a medium between the state and skills to indicate whether or not the skills can perform their target/whole actions or not. Right now, I'm just testing it using a basic Fire spell. The issue I'm having is that when the user is forced to use the chaotic skill, they cast it twice, and then go on to cast the original spell anyway. I think the issue is with how I'm calling the forced skill to be used in the state's notetags. Any advice is helpful!

NOTE: Because this relies on random numbers, I have the randomly rolled numbers set to specific values for testing. I still have the roll occur and print to the console so I know its still functioning as expected.

'Chaos Magic' State Notetags
Spoiler
Code:
<Custom Battle Effect>
target.setStateCounter(31, 1);
</Custom Battle Effect>

<Custom Turn Start Effect>
$gameSwitches.setValue(1, false);
</Custom Turn Start Effect>

<Custom Action Start Effect>
let skill = BattleManager.inputtingAction();
let chaosStacks = user.getStateCounter(31);

if (skill.item().stypeId === 2) {
    let chaosRoll = Math.randomInt(20) + 1;
    console.log(chaosRoll);
    chaosRoll = 1;
    if (chaosRoll <= chaosStacks) {
        $gameSwitches.setValue(1, true);
        let chartRoll = Math.randomInt(24, 34);
        console.log(chartRoll);
        chartRoll = 24;
        BattleManager.queueForceAction(user, chartRoll);
    }
}
</Custom Action Start Effect>

Basic Fire spell notetags
Spoiler
Code:
<target action>
if !($gameSwitches.value(1))
  perform start
  motion chant: user
  motion wait: user
  motion spell: user
  motion wait: user
  action animation
  animation wait: 4
  action effect: target
end
</target action>

Test 'chaos magic' skill notetags:
Spoiler
Code:
<target action>
perform start
motion chant: user
motion wait: user
motion spell: user
motion wait: user
action animation
animation wait: 4
action effect: target
eval: $gameSwitches.setValue(1, false);
break action
</target action>

Plugin List:
Spoiler
2024-10-01_screenshot06.png

2024-10-01_screenshot07.png
maybe put "user.clearActions();" after "BattleManager.queueForceAction(user, chartRoll);" in 'Chaos Magic' State Notetags ? It might can ensure that the original skill action is cleared from the queue, preventing it from being executed after the chaotic skill.
I'm not going to try to speak to the queueForceAction() bit, because that's added by Yanfly's Battle Engine and I'm not going to try to root through it to see how it's different from the standard force action commands from MV's code.

But your other problem is what @GlacierL said - you're saying "do this other action" but you're saying it after your original action has started. You can try clearing your action as suggested.

The other option is, since you're using action sequences, to just put it in there. Make your chaos spell effect and paste it into the action sequence of the other spells with the conditional roll.

The sucky thing is, I recall a thread about this which had notetag codes and all that, and I can't find it.
I solved it! The issue was that I should have used the normal 'forceAction()' script call instead of 'queueForceAction()'. Also, it was casting the forced skill twice because I was giving the Chaos Spell a scope of 'All Allies' and using 'action effect: target' in the action sequence rather than giving the skill no inherent scope and specifying 'action effect: friends'.
This thread is closed due to being indicated as solved. If you need it reopened, report the thread to let us know. Thank you!

Original thread: https://forums.rpgmakerweb.com/threads/help-with-a-wild-mage-style-mechanic.172313/