MV Execute code if enemy X is in a troop

Hello,

I am trying to create a code which will execute, depending on if specific enemies are members of a troop. I came up with the code below, however only thing it does, is that it activates switch 283, even if specified enemies(3, 8, 9, 11) are not in a troop.

for (i = 0; i < 6; i++) { if ($gameTroop.members()[i].enemyId() == 3 || 8 || 9 || 11) { $gameSwitches.setValue(283, true) } else if ($gameTroop.members()[i].enemyId() == 5 || 10 || 12 || 14) { $gameSwitches.setValue(284, true) } else if ($gameTroop.members()[i].enemyId() == 4 || 6 || 13) { $gameSwitches.setValue(285, true) } else if ($gameTroop.members()[i].enemyId() == 2 || 7) { $gameSwitches.setValue(286, true) } }

if ($gameTroop.members()[i].enemyId() == 3 || 8 || 9 || 11)
This says "if the member's enemy ID is 3, or if 8, or if 9, or if 11". "if X" is only false for X = 0, so "if 8" will pass even if the enemy's ID is not 3.

Try this instead:
JavaScript:
for (var nme of $gameTroop.members()) {
  var id = nme.enemyId();
  if ([3, 8, 9, 11].indexOf(id) >= 0) {
    $gameSwitches.setValue(283, true);
  } else if ([5, 10, 12, 14].indexOf(id) >= 0) {
    $gameSwitches.setValue(284, true);
  } else if ([4, 6, 13].indexOf(id) >= 0) {
    $gameSwitches.setValue(285, true);
  } else if ([2, 7].indexOf(id) >= 0) {
    $gameSwitches.setValue(286, true);
  }
}

Edit: another option could be to use a switch statement, e.g.
JavaScript:
for (var nme of $gameTroop.members()) {
  switch (nme.enemyId()) {
    case 3: case 8: case 9: case 11:
      $gameSwitches.setValue(283, true); break;
    case 5: case 10: case 12: case 14:
      $gameSwitches.setValue(284, true); break;
    case 4: case 6: case 13:
      $gameSwitches.setValue(285, true); break;
    case 2: case 7:
      $gameSwitches.setValue(286, true); break;
  }
}

if ($gameTroop.members()[i].enemyId() == 3 || 8 || 9 || 11)

This says "if the member's enemy ID is 3, or if 8, or if 9, or if 11". "if X" is only false for X = 0, so "if 8" will pass even if the enemy's ID is not 3.

Try this instead:
JavaScript:
for (var nme of $gameTroop.members()) {
  var id = nme.enemyId();
  if ([3, 8, 9, 11].indexOf(id) >= 0) {
    $gameSwitches.setValue(283, true);
  } else if ([5, 10, 12, 14].indexOf(id) >= 0) {
    $gameSwitches.setValue(284, true);
  } else if ([4, 6, 13].indexOf(id) >= 0) {
    $gameSwitches.setValue(285, true);
  } else if ([2, 7].indexOf(id) >= 0) {
    $gameSwitches.setValue(286, true);
  }
}

Edit: another option could be to use a switch statement, e.g.
JavaScript:
for (var nme of $gameTroop.members()) {
  switch (nme.enemyId()) {
    case 3: case 8: case 9: case 11:
      $gameSwitches.setValue(283, true); break;
    case 5: case 10: case 12: case 14:
      $gameSwitches.setValue(284, true); break;
    case 4: case 6: case 13:
      $gameSwitches.setValue(285, true); break;
    case 2: case 7:
      $gameSwitches.setValue(286, true); break;
  }
}
Thanks for the code and the explanation

Original thread: https://forums.rpgmakerweb.com/threads/execute-code-if-enemy-x-is-in-a-troop.160934/