Cycling Party Via Common Event Question

I'm trying to create a common event that allows players to cycle through their party with the press of a button.

I've done this successfully using EliMZ's ButtonCommonEvents plugin in conjunction with this script call (which I found in Kay_Erthisia's post from 2021) in my common event:

if ($gameParty.members().length == 3) {
$gameParty.swapOrder(0, 1)
$gameParty.swapOrder(2, 1);
}
else if ($gameParty.members().length == 2) {
$gameParty.swapOrder(0, 1);
}

It works great if I have two party members or three party members. My problem is, I'm struggling to figure out how to have a condition for the fourth party member.

I have the script for a four-person party:

if ($gameParty.members().length == 4) {
$gameParty.swapOrder(0, 1)
$gameParty.swapOrder(2, 1)
$gameParty.swapOrder(3, 1);
}

This also works well on its own, but how can I script this to change within the same common event depending on if you have 2, 3, OR 4 party members? I tried working in a label below the four-person call, skipping down to the three-person conditional script, but that didn't work and I'm not sure how to structure nesting branches (which is a chore in and of itself) in a script window.
Following the pattern, maybe this:
JavaScript:
const size = $gameParty.size();
if (size > 1) {
  $gameParty.swapOrder(0, 1);
  for (let n = 1; ++n < size;)
    $gameParty.swapOrder(n, 1);
}
I think that'll work for any (valid) party size, and it saves you using if...else or switch.

[Edit: no, it has a problem! See my following post.]
Following the pattern, maybe this:
JavaScript:
const size = $gameParty.size();
if (size > 1) {
  $gameParty.swapOrder(0, 1);
  for (let n = 1; ++n < size;)
    $gameParty.swapOrder(n, 1);
}
I think that'll work for any (valid) party size, and it saves you using if...else or switch.
Thanks for the alternate approach. It works perfectly for a party of 3 or 4, but if it's just two party members, the one that starts as the 2nd member eventually disappears after the first couple of times cycling through.
Ah, the code you quoted for party size 4 does not shift party members linearly:
  • Input: ABCD.
  • Expected: DABC?
  • swapOrder(0, 1) -> BACD;
  • swapOrder(2, 1) -> BCAD;
  • swapOrder(3, 1) -> BDAC!
The size 2 and 3 calls happen to work only because there are sufficiently few elements; the pattern is not applicable in the general case.

Try this instead:
JavaScript:
const size = $gameParty.size();
if (size > 1)
  for (let n = size; --n;)
    $gameParty.swapOrder(n, n - 1);
I.e. "bubble" the last member of the party to the front (ABCD -> DABC).

Alternatively, in the other direction:
JavaScript:
const size = $gameParty.size();
if (size > 1)
  for (let n = 0; ++n < size;)
    $gameParty.swapOrder(n, n - 1);
I.e. bubble the first member of the party to the back (ABCD -> BCDA).

If a party member "disappears", that probably means you're checking the order via map followers. Those only include battle members, but the default battle party size is 4. So if you actually have 5+ members then after 3 swaps they'll be in the reserve party. In that case, swap a few more times and they should reappear.

If you did not intend for there to be more members, check the System 1 tab and delete any extra members in the Starting Party list. You may also want to select the top entry of that list, then press the down arrow to see if there's any "no-name" actors hiding there.

Edit - alternatively, based on the code from your original post:
Spoiler
An if...else chain:
JavaScript:
const size = $gameParty.members().length;
if (size === 2) {
  $gameParty.swapOrder(0, 1);
} else if (size === 3) {
  $gameParty.swapOrder(0, 1);
  $gameParty.swapOrder(2, 1);
} else if (size === 4) {
  $gameParty.swapOrder(0, 1);
  $gameParty.swapOrder(2, 1);
  $gameParty.swapOrder(3, 1);
}
Or a switch block:
JavaScript:
switch ($gameParty.members().length) {
  case 4:
    $gameParty.swapOrder(3, 1);
    // fall-through
  case 3:
    $gameParty.swapOrder(2, 1);
    // fall-through
  case 2:
    $gameParty.swapOrder(0, 1);
    // fall-through
}
Ah, the code you quoted for party size 4 does not shift party members linearly:
  • Input: ABCD.
  • Expected: DABC?
  • swapOrder(0, 1) -> BACD;
  • swapOrder(2, 1) -> BCAD;
  • swapOrder(3, 1) -> BDAC!
The size 2 and 3 calls happen to work only because there are sufficiently few elements; the pattern is not applicable in the general case.

Try this instead:
JavaScript:
const size = $gameParty.size();
if (size > 1)
  for (let n = size; --n;)
    $gameParty.swapOrder(n, n - 1);
I.e. "bubble" the last member of the party to the front (ABCD -> DABC).

Alternatively, in the other direction:
JavaScript:
const size = $gameParty.size();
if (size > 1)
  for (let n = 0; ++n < size;)
    $gameParty.swapOrder(n, n - 1);
I.e. bubble the first member of the party to the back (ABCD -> BCDA).

If a party member "disappears", that probably means you're checking the order via map followers. Those only include battle members, but the default battle party size is 4. So if you actually have 5+ members then after 3 swaps they'll be in the reserve party. In that case, swap a few more times and they should reappear.

If you did not intend for there to be more members, check the System 1 tab and delete any extra members in the Starting Party list. You may also want to select the top entry of that list, then press the down arrow to see if there's any "no-name" actors hiding there.

Edit - alternatively, based on the code from your original post:
Spoiler
An if...else chain:
JavaScript:
const size = $gameParty.members().length;
if (size === 2) {
  $gameParty.swapOrder(0, 1);
} else if (size === 3) {
  $gameParty.swapOrder(0, 1);
  $gameParty.swapOrder(2, 1);
} else if (size === 4) {
  $gameParty.swapOrder(0, 1);
  $gameParty.swapOrder(2, 1);
  $gameParty.swapOrder(3, 1);
}
Or a switch block:
JavaScript:
switch ($gameParty.members().length) {
  case 4:
    $gameParty.swapOrder(3, 1);
    // fall-through
  case 3:
    $gameParty.swapOrder(2, 1);
    // fall-through
  case 2:
    $gameParty.swapOrder(0, 1);
    // fall-through
}
Thank you for the reply. I caught they were cycling out of order and had already tweaked that while trying to figure out how to make it work.
All of the options you've provided worked great with 4 and 3-member teams until it came down to the two-member party. The way I've been testing this is to adjust the amount of starting party members in the system tab, then starting a new game, but yes, there are quite a bit more listed in the actors tab. Is there a way to only swap the on-screen members?
You could try replacing $gameParty.size() (or $gameParty.members().length) with one of these:
JavaScript:
$gameParty.battleMembers().length
JavaScript:
$gamePlayer.followers().data().length
E.g.
JavaScript:
const size = $gameParty.battleMembers().length;
if (size > 1)
  for (let n = size; --n;)
    $gameParty.swapOrder(n, n - 1);
No luck on any combination of these. It's odd because I can cycle through groups of 3 and 4 repeatedly with absolutely no problem. It's only swapping between two people that glitches out.

Edit: Just made a liar out of myself after excessively cycling through the party of four. They eventually glitch out too.
Rethinking this a bit:

All I REALLY need to happen is call a specific party member up, have them perform an action, and shuffle them back, reversing the process, all of which can be executed in a common event, maybe 3 CE total corresponding to a skill.

Is that easier to achieve without breaking things?
No luck on any combination of these.
If you haven't already, you may want to turn off all plugins, save your project to apply Plugin Manager changes, then test. If it works OK with the plugins turned off, then you know where to start looking for the problem. 👋

after excessively cycling through the party of four. They eventually glitch out too.
The word "disappear" is more informative than "glitch out". Ideas:
  1. Is something different happening now?
  2. If not, does swapping a few dozen more times make them reappear?
  3. Does everyone disappear if you swap enough times?
  4. How many cycles did you consider "excessive"?
  5. How are you checking which actors are in your party?

All I REALLY need to happen is call a specific party member up, have them perform an action, and shuffle them back
If you still want to bubble them to and fro, then I don't see how that is any simpler than what you originally requested (i.e. bubble them 1 space forward/backward).

If you just want to swap them in/out:
Spoiler: swap in
JavaScript:
// Actor ID to swap in.
const actorId = 5;
// Get their current party position.
const index = $gameActors.actor(actorId).index();
// If they're in the party then swap them in.
if (index >= 0)
  $gameParty.swapOrder(1, index);
// Store index in variable ID 123 for later reference.
$gameVariables.setValue(123, index);
Spoiler: swap back
JavaScript:
// Get stored index for swap back.
const index = $gameVariables.value(123);
// Swap back if index is valid.
if (index >= 0)
  $gameParty.swapOrder(1, index);
You'll need to replace the values 1, 5, and 123 with numbers appropriate for your situation. I've assumed you're determining the "specific" swap-in member by their actor ID.

have them perform an action
Hold up. Are you doing this in battle? Because if so, then like I said earlier, you will also need to refresh the battle status after battle party member changes. If the swapped-in member was not already in the active battle party, it would also be a good idea to initialise their battle properties. Maybe:
JavaScript:
// refresh display
$gameTemp.requestBattleRefresh();
// init battle stuff for actor ID 5
$gameActors.actor(5).onBattleStart();
If you haven't already, you may want to turn off all plugins, save your project to apply Plugin Manager changes, then test. If it works OK with the plugins turned off, then you know where to start looking for the problem. 👋


The word "disappear" is more informative than "glitch out". Ideas:
  1. Is something different happening now?
  2. If not, does swapping a few dozen more times make them reappear?
  3. Does everyone disappear if you swap enough times?
  4. How many cycles did you consider "excessive"?
  5. How are you checking which actors are in your party?


If you still want to bubble them to and fro, then I don't see how that is any simpler than what you originally requested (i.e. bubble them 1 space forward/backward).

If you just want to swap them in/out:
Spoiler: swap in
JavaScript:
// Actor ID to swap in.
const actorId = 5;
// Get their current party position.
const index = $gameActors.actor(actorId).index();
// If they're in the party then swap them in.
if (index >= 0)
  $gameParty.swapOrder(1, index);
// Store index in variable ID 123 for later reference.
$gameVariables.setValue(123, index);
Spoiler: swap back
JavaScript:
// Get stored index for swap back.
const index = $gameVariables.value(123);
// Swap back if index is valid.
if (index >= 0)
  $gameParty.swapOrder(1, index);
You'll need to replace the values 1, 5, and 123 with numbers appropriate for your situation. I've assumed you're determining the "specific" swap-in member by their actor ID.


Hold up. Are you doing this in battle? Because if so, then like I said earlier, you will also need to refresh the battle status after battle party member changes. If the swapped-in member was not already in the active battle party, it would also be a good idea to initialise their battle properties. Maybe:
JavaScript:
// refresh display
$gameTemp.requestBattleRefresh();
// init battle stuff for actor ID 5
$gameActors.actor(5).onBattleStart();
Apologies for the delay. I will definitely try the swapping script when next I'm able to work on my project and thank you so much for the input.
To be more specific about what I'm trying to do and what the set up is:
I'm using KageDesu's ABS system, so "battle" looks a bit different, but it would basically be an on-map swap.
That said, the purpose is I want certain actors to be the only ones who can perform certain functions: i.e. run and jump to clear several tiles or otherwise pass through tiles that any other character can't. I envisioned this working best by using a common event tied to a key that gathers the followers, swap the specific actor (who wouldn't be in a predictable position in the party, but required to be in the party for the command to work), bring them forward as the "leader", execute the action, then cycle back to their spot behind the main character.
By glitch out - I meant that the character sprite would disappear leaving only a shadow and occasionally two pixels around where their leg would be.
Regarding the comment about checking if an actor is in the party, I'm doing that in a conditional branch. Is that what you meant?
Regarding "Bubbling" I'm not sure what that would be.
I'm using KageDesu's ABS system, so "battle" looks a bit different, but it would basically be an on-map swap.
In that case I would guess you'd need script calls specific to KageDesu's Alpha ABS.

By glitch out - I meant that the character sprite would disappear leaving only a shadow and occasionally two pixels around where their leg would be.
That certainly sounds plugin-specific.

Regarding the comment about checking if an actor is in the party, I'm doing that in a conditional branch.
I didn't phrase that very well, sorry. I meant: how are you checking the results of the swap? From what you've said, it sounds like you're looking at the ABS party sprites on the map? In that case I'd guess you just need to figure out how to refresh them appropriately. I just skim-read the plugin code and this looks promising (untested):
JavaScript:
SceneManager._scene._spriteset.refreshAfterABS();
It looks like that might refresh the relevant sprites.

Regarding "Bubbling" I'm not sure what that would be.
What I mean by bubbling an element of an array to a given index is to move it to that index without removing any other element, which involves shifting all elements between the two indices. That can be done by repeated swapping of adjacent elements or by removing the element, performing an index shift, then replacing the element. Pairwise swapping can be visualised as a bubble rising through water: the water above ends up below the bubble. Hence the term, found in things like the "bubble sort" algorithm.
In that case I would guess you'd need script calls specific to KageDesu's Alpha ABS.


That certainly sounds plugin-specific.


I didn't phrase that very well, sorry. I meant: how are you checking the results of the swap? From what you've said, it sounds like you're looking at the ABS party sprites on the map? In that case I'd guess you just need to figure out how to refresh them appropriately. I just skim-read the plugin code and this looks promising (untested):
JavaScript:
SceneManager._scene._spriteset.refreshAfterABS();
It looks like that might refresh the relevant sprites.


What I mean by bubbling an element of an array to a given index is to move it to that index without removing any other element, which involves shifting all elements between the two indices. That can be done by repeated swapping of adjacent elements or by removing the element, performing an index shift, then replacing the element. Pairwise swapping can be visualised as a bubble rising through water: the water above ends up below the bubble. Hence the term, found in things like the "bubble sort" algorithm.
You've been so helpful, thank you.

For what it's worth, this script:

JavaScript:
// Actor ID to swap in.
const actorId = 5;
// Get their current party position.
const index = $gameActors.actor(actorId).index();
// If they're in the party then swap them in.
if (index >= 0)
$gameParty.swapOrder(1, index);
// Store index in variable ID 123 for later reference.
$gameVariables.setValue(123, index);

JavaScript:
// Get stored index for swap back.
const index = $gameVariables.value(123);
// Swap back if index is valid.
if (index >= 0)
$gameParty.swapOrder(1, index);

solved my immediate problem of putting the right actor in control of the action in my common event, then cycled them back to where they needed to be. I used it alongside TyrusWoo's Follower Control Plugin. It's not perfect, but it will get me down the road for now.

I've reached out for additional guidance on the graphical issue on KageDesu's Discord.

Thank you again for all of your help.

I'd consider this matter resolved.
Good luck! 👋

Original thread: https://forums.rpgmakerweb.com/threads/cycling-party-via-common-event-question.164585/