Script call for Troop Index

How do I check an enemies troop index number?

Like if I was making something like this:


Code:
if $gameTroop.members()[1]
 
else
if $gameTroop.members()[2]
  
else
if $gameTroop.members()[3]
    
end

I want the enemy to act a certain way in battle based on which enemy index number it is.
easy,
but i need to know what context you are working in, a plugin or a scriptcall in battle?

lets assume you are making a plugin and you want to do something to the enemy during while alive battle
can get that instance of the enemy like this:


Code:
this; //for within the game enemy class

you can find its index like this
Code:
var troopIndex = $gameTroop.members().findIndex(this);

now findIndex() is an Array function that will get an index from an array. it will return -1 if that enemy doesn't exist within that array. (possibly if its dead)
once we have our index we can use it to do something

Code:
if(troopIndex === 5) { //we know its a number so we might as well use strict comparison
//do something
}

Original thread: https://forums.rpgmakerweb.com/threads/script-call-for-troop-index.101866/