Party Member Troop Position as a Variable Problem

Hey guys! So as the title says, I need some help trying to get the position of actors in the battle into a variable. I have actually figured out a way of doing so by doing the following script call:


$gameVariables.setValue(30, $gameParty.members()[3].actorId());


However the problem I run into is what if there is no actor in that position? In this case its the 4th position (with 0 being position 1). When I run the script call I get an error saying that actorId is undefined because there is no actor in that position. Is there a way for me to still perform a check if there is an actor in that position and if not then continue with the rest of the script call?


This is essentially what I'm trying to do:


Actor ID #1 uses a Skill which performs the following:


$gameActors.actor($gameVariables.value(29)).addState(72);


Then a common event plays which does the last bit of the skill:


$gameVariables.setValue(30, $gameParty.members()[0].actorId());


if ($gameVariables.value(30) === $gameVariables.value(29)) {


$gameParty.members()[0].startAnimation(140);


}


$gameVariables.setValue(31, $gameParty.members()[1].actorId());


if ($gameVariables.value(31) === $gameVariables.value(29)) {


$gameParty.members()[1].startAnimation(140);


}


$gameVariables.setValue(32, $gameParty.members()[2].actorId());


if ($gameVariables.value(32) === $gameVariables.value(29)) {


$gameParty.members()[2].startAnimation(140);


}


$gameVariables.setValue(33, $gameParty.members()[3].actorId());


if ($gameVariables.value(33) === $gameVariables.value(29)) {


$gameParty.members()[3].startAnimation(140);


}


tldr; So basically I have a skill call a common event where the character that used a skill that has its own actor ID as variable #29 and then adds a state to that character. The second part of the skill then plays an animation on the character. But if there is no actor in that troop position I get an error. I'm also not a super advanced javascript user so please pardon me for my noobie looking script ^_^. Any help would be greatly appreciated! 
You can use $gameParty.size() to see how many members in the party, and only do the variable assignment if the party is big enough for someone to be in that slot (and maybe set the variable to 0 if there's nobody there, just so it's not left over from when you might have had a larger party).
You can use $gameParty.size() to see how many members in the party, and only do the variable assignment if the party is big enough for someone to be in that slot (and maybe set the variable to 0 if there's nobody there, just so it's not left over from when you might have had a larger party).

Thanks so much! This works perfectly now. 🙂 🙂 🙂

Original thread: https://forums.rpgmakerweb.com/threads/party-member-troop-position-as-a-variable-problem.66821/