HELP! Can you help me identify whats wrong with my Leader Cycle System?

Hi all!

Can someone please try and help me? I Use a system where you can cycle through the party so you can chose who is your leader.

To cycle to the right I have the following:

Code:
function partycycleR() {
for (var i = 0; i < $gameParty.members().length - 1; i += 1) {
    $gameParty.swapOrder(i, i+1)
}
}
This cycle through the right and when it goes to the last party member it starts over with the party member at 0 index.


To cycle to the left i have this:

Code:
function partycycleL() {
last = $gameParty.members().length - 1
another = 1
 $gameParty.swapOrder(0, last)
for (var i = 0; i < $gameParty.members().length - 2; i += 1) {
    $gameParty.swapOrder(last - i, last - another)
    another += 1
}
}

This does the exact same thing but in reverse, (to the left).
Now, everything works super fine, I can add more party members and it works, BUT NOT when I remove members, when I remove, SOMETIMES it works, and sometimes it doesn't.

When it goes wrong, BOTH functions go wrong and they skip members.
So I dont know why removing members SOMETIMES screws the functions .

Hope someone can catch where im wrong.

THANKS!
Do you mean if you remove a party member, calling these functions afterwards doesn't cycle them as expected?
For one thing, when you use variables (last and another) you should declare them. When you don't, they have global scope which is bad practice and potentially interfering with other pieces of code.

For another, I don't see what either of these have to do with adding or removing party members. Are you saying that they work or not depending on how many members you have?
Thanks both of you for your fast reply. @ATT_Turan Yes, Im sorry, I still have bad practices on my code, I always leave everything at global scope, ha, I know I shouldt.
@Trihan and @ATT_Turan YES! Thats why im going crazy, I dont think any of the functions should be affected depending on the number of members.

What I have tested so far:
1. It ALWAYS work if I add members.
2. It might not work when I remove members.
3. When the cycle start doing it wrong (not cycling as it should) If I remove ALL members and add them again, both functions will work again.

This make no sense right?
Thanks both of you for your fast reply. @ATT_Turan Yes, Im sorry, I still have bad practices on my code, I always leave everything at global scope, ha, I know I shouldt.
@Trihan and @ATT_Turan YES! Thats why im going crazy, I dont think any of the functions should be affected depending on the number of members.

What I have tested so far:
1. It ALWAYS work if I add members.
2. It might not work when I remove members.
3. When the cycle start doing it wrong (not cycling as it should) If I remove ALL members and add them again, both functions will work again.

This make no sense right?
It's going to make sense somewhere along the chain, as computers can't do anything but what we tell them to.

Can you give me an example of your exact steps in terms of removing a party member and calling these functions, including whereabouts the call is being executed?
Sure.
Here is a video of what is happening:

You can see how I cycle, no problem, and after the event of the cat, I cant cycle properly any more.

So, I have 3 actors, and I cycle though them no problem. Then I summon the cat, that's where I add another actor(The cat is an actor):
Capture.JPG

Then I play with the cat, at this moment I have 4 actors (I could cycle with no problem at this time if i want to). Then I de-summon the cat:
Capture2.JPG

So the cat goes back, I remove him. The last actor is the one it was before (the girl) and my party is again of 3 members. But now When I cycle, its skips the actor 2 (the blue hair one) I know its skipping him because I have to call the function twice to move to the next character. And it goes from the girl to the boy, if a call the function again it goes back to the girl, and i need to call the function twice again to go back to the boy, but always skipping the blue haired actor.

Hope this helps ha.
To cycle to the left i have this:

Code:
function partycycleL() {
last = $gameParty.members().length - 1
another = 1
 $gameParty.swapOrder(0, last)
for (var i = 0; i < $gameParty.members().length - 2; i += 1) {
    $gameParty.swapOrder(last - i, last - another)
    another += 1
}
}
This logic is wrong.

Do it out step by step - your last and another stuff overlaps and changes the order around. It would be worse with fewer than 4 members, that's why you're getting math that skips actors in the order.

Here's an example, following through step by step.
Starting:
a b c d

Cycling one to what you're calling left should produce
d a b c

Following your function:

swap(0, last)
d b c a

i = 0, last = 3, another = 1: 3-0 = 3, 3-1 = 2
swap(3, 2)
d c b a

i = 1, last = 3, another = 2: 3-1 = 2, 3-2 = 1
swap(2, 1)
d b c a

i = 2 loop is broken. End result is
d b c a
As you can see, that's entirely the wrong order.

All you need to do is the same thing you have for cycling right, but going backwards.

Code:
function partycycleR() {
    for (var i = $gameParty.members().length-1; i > 0; i--) {
        $gameParty.swapOrder(i, i-1)
    }
}

a b c d

i = 3
a b d c

i = 2
a d b c

i = 1
d a b c

i = 0 loop is broken. End result is d a b c
Hmmm.... I changed my "left" cycle function as you told me. But it doesn't work as it is supposed to.

Initial Setup
a b c
I press the button nothing, happens.
a b c
I press the button again, it goes as it should.
c a b
I press the button again, it goes as it should.
b c a
I press the button again, it goes as it should
a b c
And it repeats the above, once the initial setup is present, I press the button and nothing happens, I have to press it again so it goes to c a b and bla bla....


And not only that, my "right" function doesnt owrk any more. Once I tried my left function:

Initial setup
a b c
I press the button nothing, happens.
a b c
I press the button again, it goes as it should.
b a c
I press the button again, and it goes back to initial setup (a b c) where it should go (c a b)
And it gets stuck there, not showing c ever.
Sorry, I don't know what else to tell you. It does seem like you have something else in your project that's borking this up.

Even the video and events you posted above are confusing to me, the events and your description of events and what I see in the video don't all match.

Good luck.
Yeah, I was thinking to make a new project with nothing on it and test this, its quite possible I have meddled so much with my current project that there is something getting in the way.

Thank you for taking your time again =)
Maybe I'm missing something, but why is it necessary to change the entire party's indexes instead of simply swapping the party current leader with either the second or last member?

a loop like $gameParty.swapOrder(i, i++) or $gameParty.swapOrder(i, i--) isn't gonna work, because the last or first iteration uses an index that doesn't exist in the array.
you'd need to add either if (i == n) $gameParty.swapOrder(n, 0) or if (i == 0) $gameParty.swapOrder(0, n) to avoid that
Maybe I'm missing something, but why is it necessary to change the entire party's indexes instead of simply swapping the party current leader with either the second or last member?
Because that inherently doesn't preserve the rest of the order.

If you have
a b c d

and you want to cycle that by one,
b c d a

is not what you'd get from a single swap command.

a loop like $gameParty.swapOrder(i, i++) or $gameParty.swapOrder(i, i--) isn't gonna work, because the last or first iteration uses an index that doesn't exist in the array.
That's not true, if you read the code that was written above it uses the array length-2 and i>0 respectively so that +1 and -1 always refer to legitimate array elements.
That's not true, if you read the code that was written above it uses the array length-2 and i>0 respectively
whoops, I did miss that.

what if you removed and then added the party leader again? wouldn't that achieve the same thing he's trying to do?
what if you removed and then added the party leader again? wouldn't that achieve the same thing he's trying to do?
Only in one direction - cycling the other direction requires swapping (or removing everyone, reordering them externally, and re-adding them).

The functions above should work. They do for me when I paste them into the console.

Original thread: https://forums.rpgmakerweb.com/threads/help-can-you-help-me-identify-whats-wrong-with-my-leader-cycle-system.162106/