Switch Actors when leader is KO'd outside of battle?

This is something that has been on my mind for the longest time now, but I want to make it possible for the party's leader to change when their HP hits 0. Namely when the leader's HP runs out on the field, they'll be swapped with the next healthy Actor in the lineup, making them leader. OR if there's nobody else, it triggers an event instead!

But i'm not sure how I could go about doing that. Is it possible to do so with eventing? If not, is there a plugin out there that will allow what i'm looking for? Apologies if this is in the wrong section, but i'd appreciate any aid!
how do you expect the HP to run out outside the battle?

This usually means that there is an event that causes HP damage, and you can simply use the event that causes the damage to change party order or remove the first actor from the party or whatever.

if the damage isn't done by events then there are a few other options, but it depends on how the actor gets the damage, or requires a parallel process that will add to lag.
$gameParty.swapOrder(0,$gameParty.members().indexOf($gameParty.members()[1]))
Use that in conditional branches that check Hp. Keep in mind that while this will work instantly, you’ll have to refresh the scene to make the sprites visually change.

Edit: no scene refresh is needed. I was thinking of the “splice” function.
how do you expect the HP to run out outside the battle?

This usually means that there is an event that causes HP damage, and you can simply use the event that causes the damage to change party order or remove the first actor from the party or whatever.

if the damage isn't done by events then there are a few other options, but it depends on how the actor gets the damage, or requires a parallel process that will add to lag.

That is precisely what I intended when I asked about this. I want on-map events that can damage the player (traps, wrong choices, etc.) and, if it KOs them, force their leading Actor to switch with another(Or trigger a negative event if there's no one available).

$gameParty.swapOrder(0,$gameParty.members().indexOf($gameParty.members()[1]))
Use that in conditional branches that check Hp. Keep in mind that while this will work instantly, you’ll have to refresh the scene to make the sprites visually change.

Edit: no scene refresh is needed. I was thinking of the “splice” function.

Oh, thank you! I'll give it a try. 🙏
While i'm still here, is there a script call that checks the (leading) Actor's HP? I'm not very good with scripts, i'm afraid...
$gameParty.members()[0].hp for current hp
$gameParty.members()[0].mhp for max hp
$gameParty.members()[0].hp for current hp
$gameParty.members()[0].mhp for max hp

Splendid! Thank you once more. 🙏
While i'm still here, is there a script call that checks the (leading) Actor's HP? I
no need for a script call, it can be done with eventing alone.
the script variant is easier, but the event variant can do much more especially if you have problems with scripts.

control variable - game data section (the one that defaults to map id) can do much more if you know how.
to get the HP of the first actor (or any actor) you can use the command to get the ID of any actor in the first (second, third, ...) position and then use that ID to get that actors HP, MP, ATK, etc.

Original thread: https://forums.rpgmakerweb.com/threads/switch-actors-when-leader-is-kod-outside-of-battle.133735/