MV "Force multiple enemies to use skill" script not working

Hello. I'm having a problem with a script call i'm making.
This one is supposed to be executed at the start of a battle (Turn 0)
JavaScript:
for(var i = 0; i < $gameTroop._enemies.length; i++)
{
   var enemy = $gameTroop._enemies[i];
   if(enemy.isStateAffected(115))
   {
      enemy.forceAction(113, -1);
      BattleManager.forceAction(enemy);
      this.setWaitMode('action');
   }
}

Basically, this script checks which enemies are afflicted by x State Effect, and the game will force those enemies to use x attack. The way it's written here does not work, however, and instead only forces the last enemy in the troop to use the called attack. Am i missing something?
One thing I'd point out is you probably shouldn't use the _enemies member variable.

One because it's bad programming style/habit to go directly for the member variables when you have accessor methods available. And two because it could cause undesired results. I'm not sure what might happen when you start checking for states or trying to force actions on enemies in the troop who have died.

It would be, if nothing else, more efficient to use $gameTroop.aliveMembers()

I'm not positive on your primary issue - I think it may be a timing issue, where the setWaitMode is insufficient separation and the multiple force action commands are getting queued at the same time, which causes only the most recent to actually happen.
One thing I'd point out is you probably shouldn't use the _enemies member variable.

One because it's bad programming style/habit to go directly for the member variables when you have accessor methods available. And two because it could cause undesired results. I'm not sure what might happen when you start checking for states or trying to force actions on enemies in the troop who have died.

It would be, if nothing else, more efficient to use $gameTroop.aliveMembers()

I'm not positive on your primary issue - I think it may be a timing issue, where the setWaitMode is insufficient separation and the multiple force action commands are getting queued at the same time, which causes only the most recent to actually happen.

For the first issue, checking for alive members should not be an issue since this command is executed at the very start of the battle.
For the second issue i was thinking of something like that, so i should find a way to make the game wait for every action to be completed.
why use a script at all?
why not simply use a troop event at turn 0 with the event commands?

One advantage of the event commands is that they contain all the required internal checks and waits to handle everything correctly.
I can see using the script equivalent of the commands if you need some settings that are not available in the event command (there are more than enough cases for that), but in this specific code I don't see any need for doing it in script.

you should at least try it for testing purposes in case the error comes from a different place than you think.
why use a script at all?
why not simply use a troop event at turn 0 with the event commands?

One advantage of the event commands is that they contain all the required internal checks and waits to handle everything correctly.
I can see using the script equivalent of the commands if you need some settings that are not available in the event command (there are more than enough cases for that), but in this specific code I don't see any need for doing it in script.

you should at least try it for testing purposes in case the error comes from a different place than you think.
yeah, i was having a bit of a brain fart there. I've been working with my game to make the battle system as dynamic as possible, but unless i implement a summon system, this is one of those things that doesn't need a script.
For the first issue, checking for alive members should not be an issue since this command is executed at the very start of the battle.
Fair enough! But good habits and best practice are what they are 😉

why use a script at all?
why not simply use a troop event at turn 0 with the event commands?
I assume for modularity. If you make it an event, you have to actually create the event in each troop, making the conditionals and commands refer to the enemies actually in that troop.

If made into a functioning script, that can just be pasted into every troop and it doesn't matter how many enemies or which enemies are in there.
you should at least try it for testing purposes in case the error comes from a different place than you think.
I agree.
The problem with that script call is that interpreter waiting (e.g. via setWaitMode) happens between commands. Also, by default the BattleManager only remembers up to 1 "forced action subject" at a time. So the loop completes in 1 frame, then the BattleManager sees that there's someone (the last enemy in the troop) waiting to do a forced action.

You could get around that by eventing the loop part using Loop & Break Loop commands. The loop counter, i, could be swapped for a game variable (Control Variables & $gameVariables.value(id)).
Thanks to caethyril's idea, i managed to make it work!

Code:
◆Loop
  ◆Script:var id = $gameVariables.value(32);
  :      :var enemy = $gameTroop.members()[id];
  :      :
  :      :if(enemy.isStateAffected(115))
  :      :{
  :      :   enemy.forceAction(113, -1);
  :      :   BattleManager.forceAction(enemy);
  :      :   this.setWaitMode('action');
  :      :}
  ◆Control Variables:#0032 StinkEnemies += 1
  ◆If:Script:$gameVariables.value(32) >= $gameTroop.members().length
    ◆Break Loop
    ◆
  :End
  ◆
:Repeat Above

This code does exactly what i needed. Thanks for the help!

ATT_Turan said:

I assume for modularity. If you make it an event, you have to actually create the event in each troop, making the conditionals and commands refer to the enemies actually in that troop.

If made into a functioning script, that can just be pasted into every troop and it doesn't matter how many enemies or which enemies are in there.
That's exactly it. And thanks to one of Yanfly's plugins, i only have to past it on the first troop battle to make it work for all of them.
Great! Remember to set the variable to 0 before the loop starts, too. 👋

Original thread: https://forums.rpgmakerweb.com/threads/force-multiple-enemies-to-use-skill-script-not-working.147359/