MZ [solved] Checking if a party member in a specific spot is dead or not?

Hello again!
This is a follow on from the script I mentioned in my previous thread.
Basically, I'm using VisuStella's Common Event Button plugin to swap the positions of party members around for use in Jragyn's JABS plugin.

This is the script I have been using:
JavaScript:
\\Checking Party size and switching accordingly
if ($gameParty.members().length == 3) {
   $gameParty.swapOrder(0, 1)
   $gameParty.swapOrder(2, 1);
} else if ($gameParty.members().length == 2) {
     $gameParty.swapOrder(0, 1);
} else {
}
Which works well for switching!

However, this lets me switch out a character who is dead, and continue attacking with them which is... not quite ideal?

I wrote up an idea of how I'd set this up:
Code:
if I have 3 party members
    if member 1 is dead
        if member 2 is dead
            do nothing
        else
            Swap member 0 and 2
    else
        swap member 0 and 1
        swap member 1 and 2
else if I have 2 Party members
    if member 1 is dead
        Do nothing/ play buzz
    else
        Swap member 0 and 1
else //1 member
    Do nothing / play buzz

I'm sure there are FAR cleaner ways to do this, but this is all my Greymatter could come up with at this point.
I've been rummaging around in the RPG Maker MV/ MZ Script calls excel file (such beauty!) and figured I could probably use ".isStateAffected(1)" to check for death.
But this is where it gets a bit beyond me...

My attempt:
JavaScript:
if ($gameParty.members().length == 3) {
    if $gameParty.members()[1].isStateAffected(1) {
        if $gameParty.members()[2].isStateAffected(1) {
        } else {
            $gameParty.swapOrder(0, 2)
        }
    }else {
        $gameParty.swapOrder(0, 1)
        $gameParty.swapOrder(1, 2)
    }
} else if ($gameParty.members().length == 2) {
    if $gameParty.members()[1].isStateAffected(1) {
    } else {
        $gameParty.swapOrder(0, 1)
    }
} else {
}

Returns the following error:
JavaScript:
SyntaxError: Unexpected identifier
    at Game_Interpreter.command355 (rmmz_objects.js:11265)
    at Game_Interpreter.<computed>.<computed> [as command355] (VisuMZ_0_CoreEngine.js:6411)
    at Game_Interpreter.executeCommand (eval at onScriptLoad (FOSSIL.js:7880), <anonymous>:195:26)
    at Game_Interpreter.update (rmmz_objects.js:9539)
    at Game_Map.updateInterpreter (rmmz_objects.js:6769)
    at Game_Map.update (rmmz_objects.js:6673)
    at Game_Map.update (J-ABS.js:3364)
    at Scene_Map.updateMain (rmmz_scenes.js:733)
    at Scene_Map.<computed> [as updateMain] (VisuMZ_4_Debugger.js:404)
    at Scene_Map.updateMainMultiply (rmmz_scenes.js:729)
Which I honestly have no clue what to do with.

I'm assuming I'm mishandling "$gameParty.members()[]" and need to do something else with it before I can check if that party member has a state?

Any and all advice would be incredibly appreciated! Thank you!
What exactly are you trying to do? Just move the leader to the end and shuffle everyone else up one spot? What should happen with dead party members? What if there are a couple of dead party members?

If that's all you want, you don't need to do all of that at all - you just need to remove the first one and add them back to the end, and repeat if the new first member is dead:

Code:
Loop
  Script: $gameParty._actors.push($gameParty._actors.shift());
  If: Script: $gameParty.leader().isAlive()
    Break Loop
  End
End Loop
Script: $gamePlayer.refresh();
        $gameMap.requestRefresh();


It will eventually shuffle ALL party members, not just the battler party, but if you have on-map battles with only one actor visible, I'm guessing that's what you're after.

Note - if you run this and ALL party members are dead, you will get into an infinite loop. So you would need to be sure if that happened, you got gameover (or whatever the desired result is) and didn't go into this loop at all. If you specifically need to cater for that, change the condition to this:

Code:
If: Script: $gameParty.leader().isAlive() || $gameParty.isAllDead()
Oh my goodness @Shaz you incredible human being. I... hadn't even considered using a loop.
Nor did I even know about half of the functions you've used there! This looks so much more beautiful and clean than anything I could have dreamed of! :LZSooo:

Guess I still have a looong way to go in my "maybe I can learn JS?" journey. XD

It wasn't quite working when I first tried it, but I moved the refresh script into the loop and now it works fine!
1635656100868.png


Thank you so much!!
Seriously, I hope you know you're fantastic! 😀
Please check my post again. I did some edits while you were obviously reading, and have improved it a bit.

Odd that it didn't work when the refresh was outside of the loop.


Thread closed

Original thread: https://forums.rpgmakerweb.com/threads/solved-checking-if-a-party-member-in-a-specific-spot-is-dead-or-not.141693/