MV Help with Passive States Maximum call stack size exceeded (MV)

I'm trying to make a conditional passive state that only activates if the two party members that have the passive state are in the first four slots of the party. (Out of 10 party slots.)

Code:
<Custom Passive Condition>
condition=$gameActors.actor(5).index() < 4 && $gameActors.actor(6).index() < 4;
</Custom Passive Condition>

The above notetag on the state itself works but only outside of battle, when in battle both party members have the state applied no matter where they are in the party alignment.
But the real problem is obviously that I get nearly 100 error messages in the console when I try to test and I am a noob when it comes to trying to solve it. I've tried toggling all my plugins but it seems like even when the AutoPassiveStates plugin is the only plugin on, the errors still appear. Pls help, sorry if dumb question. I also have the PartySwitch plugin aswell if it helps.

Screenshot 2023-10-19 230913.png
Try this.
Code:
condition = $gameParty.members().indexOf($gameActors.actor(5))<4 && $gameParty.members().indexOf($gameActors.actor(6))<4;
I have not looked at this specific plugin, but I've seen similar issues before, so it might be another special case and worth a try:

The game will create each actor when it is first referenced, so what usually happened in previous cases was:
- Actor #5 is referenced
- $gameActors realizes that the entry does not yet exist and creates a new one
- During initialization the passive conditions are checked
- The conditions reference Actor #5
- Because the initialization is not yet completed, the entry does not exist yet and $gameActors creates a new one
- ...

A quick way to fix that would be to check if the data entry for the actor already exists and skip the condition if it doesn't:
Code:
let member1Present = $gameActors._data[5] && $gameActors.actor(5).index() < 4;
let member2Present = $gameActors._data[6] && $gameActors.actor(6).index() < 4;
condition = member1Present && member2Present;
@Another Fen Good idea! I don't think any of this code should be getting executed while the game is initializing, the passive state conditions shouldn't be looked at until you're in-game and it's evaluating whether to make the state active.

But the reasoning for exceeding the call stack makes sense, so see if that works.
I have not looked at this specific plugin, but I've seen similar issues before, so it might be another special case and worth a try:

The game will create each actor when it is first referenced, so what usually happened in previous cases was:
- Actor #5 is referenced
- $gameActors realizes that the entry does not yet exist and creates a new one
- During initialization the passive conditions are checked
- The conditions reference Actor #5
- Because the initialization is not yet completed, the entry does not exist yet and $gameActors creates a new one
- ...

A quick way to fix that would be to check if the data entry for the actor already exists and skip the condition if it doesn't:
Code:
let member1Present = $gameActors._data[5] && $gameActors.actor(5).index() < 4;
let member2Present = $gameActors._data[6] && $gameActors.actor(6).index() < 4;
condition = member1Present && member2Present;
Putting this code into the state stops the errors from appearing on the title screen but they return once I start a new game, I don't know if I was supposed to put it into the state or not but now I'm getting this error instead. I hope I did this right but I'm at a loss again.
Screenshot 2023-10-20 123241.png
It seems weird to me that these errors are coming from the Options Core.

I can also see that there are errors unrelated to this state coming from your Options Core.

Are you trying to calculate buffs or parameters based on this state? That would be a reason that you're breaking the call stack, if it's trying to reference the calculation for a parameter based on itself.

I think you should turn off Auto Passive States and fix any errors you see without this being involved at all.

Then, similarly, turn off the Options Core and test this passive state. It looks like something hinky is going on.
Actually it seems that when I have Auto Passive States as the only plugin, the passive state itself doesn't even function properly. The state works perfectly fine outside of battle but when in battle the state starts unactivated until that party member takes their turn where it immediately activates for the rest of the battle.
Also, having every plugin activated except Auto Passive States gives no errors at all. And the only time errors start to appear is when I turn YEP_PartySystem on. Having every plugin but Party System gives no errors, but the passive states stops functioning.
The only notetag that the state has is the code Another Fen gave earlier.
Even when I tried this in a new project with only the one or both plugins installed, I still get the same problems and errors.
can we see your plugin database, because I get the feeling your plugin list
from YEP aren't in the EXACT order as they should be.
If these are in the wrong order then I must be blind.

Screenshot 2023-10-20 175900.png
Actually it seems that when I have Auto Passive States as the only plugin, the passive state itself doesn't even function properly. The state works perfectly fine outside of battle but when in battle the state starts unactivated until that party member takes their turn where it immediately activates for the rest of the battle.
Hadn't thought of this, but the index function will return -1 if the actor is not in the party at all (which might happen during battle where the party is reduced to the battle members). An easier alternative to check whether your actor is in the battle party could be this:
$gameParty.battleMembers().contains($gameActors.actor(5))

The passive conditions will likely only get updated when the actors stats or traits change (hence the delay before the state is applied during battle). If you need to update your passive states manually you can try to call
$gameActors.actor(5).refresh();
or add more update trigger points through a plugin.

Putting this code into the state stops the errors from appearing on the title screen but they return once I start a new game, I don't know if I was supposed to put it into the state or not but now I'm getting this error instead.
The screenshot does not really show what exactly causes the excessive amount of calls, most of the errors seem to come from the stack being at its limit and overflowing in random spots.

Just guessing for now, but does the error occur if you disable the passive condition? (Or change it to something simple like condition = true; ?)

If that actually avoids the error, you could try to set the condition to this instead in case some other "non-existing" party member causes the infinite loop:
Code:
condition = (!a.isActor() || $gameActors._data[a.actorId()]) &&
    $gameParty.battleMembers.contains($gameActors.actor(5)) &&
    $gameParty.battleMembers.contains($gameActors.actor(6));
(This should disable the condition for any actor during their initialization rather than just actor 5 and 6).
Hadn't thought of this, but the index function will return -1 if the actor is not in the party at all (which might happen during battle where the party is reduced to the battle members).
That shouldn't be an issue if the OP is using the code I provided in post #2, because the members() method, when used in battle, automatically refers to battleMembers.

There must be some kind of hinky interplay going on because there are errors in the above screenshot about parameters. That's not coming from anything in this passive state code, and if things are configured correctly the mere presence of Auto Passive States wouldn't cause that.
Hadn't thought of this, but the index function will return -1 if the actor is not in the party at all (which might happen during battle where the party is reduced to the battle members). An easier alternative to check whether your actor is in the battle party could be this:
$gameParty.battleMembers().contains($gameActors.actor(5))

The passive conditions will likely only get updated when the actors stats or traits change (hence the delay before the state is applied during battle). If you need to update your passive states manually you can try to call
$gameActors.actor(5).refresh();
or add more update trigger points through a plugin.


The screenshot does not really show what exactly causes the excessive amount of calls, most of the errors seem to come from the stack being at its limit and overflowing in random spots.

Just guessing for now, but does the error occur if you disable the passive condition? (Or change it to something simple like condition = true; ?)

If that actually avoids the error, you could try to set the condition to this instead in case some other "non-existing" party member causes the infinite loop:
Code:
condition = (!a.isActor() || $gameActors._data[a.actorId()]) &&
    $gameParty.battleMembers.contains($gameActors.actor(5)) &&
    $gameParty.battleMembers.contains($gameActors.actor(6));
(This should disable the condition for any actor during their initialization rather than just actor 5 and 6).
Code:
<Custom Passive Condition>
let member1Present = $gameActors._data[5] && $gameActors.actor(5).index() < 4;
let member2Present = $gameActors._data[6] && $gameActors.actor(6).index() < 4;
if (member1Present && member2Present) {
   condition = (!a.isActor() || $gameActors._data[a.actorId()]) &&
    $gameParty.battleMembers().contains($gameActors.actor(5)) &&
    $gameParty.battleMembers().contains($gameActors.actor(6));
} else {
   condition = false;
}
</Custom Passive Condition>

Okay getting somewhere now!
The above code does set the two chosen actors to have a passive state only when they are in the first four slots of the party. (The actual party members participating in the fight.) Now it even works inside of battle as well, but at the cost of a half second of lag when switching members with the actorPartySwitch plugin which isn't THAT big of a deal.
However, I still get a load of errors every time I start a new game or load a game, but I mean once you get past the 10 seconds of nonstop errors the system works like a charm.
I also added extra states to test more options for this type of passive state with more party members or conflicting members.

Code:
<Custom Passive Condition>
let member1PresentB = $gameActors._data[2] && $gameActors.actor(2).index() < 4;
let member2PresentB = $gameActors._data[4] && $gameActors.actor(4).index() < 4;
let member3PresentB = $gameActors._data[6] && $gameActors.actor(6).index() < 4;
if (member1PresentB && (member2PresentB && member3PresentB)) {
   condition = (!a.isActor() || $gameActors._data[a.actorId()]) &&
    $gameParty.battleMembers().contains($gameActors.actor(2)) &&
    $gameParty.battleMembers().contains($gameActors.actor(4)) &&
    $gameParty.battleMembers().contains($gameActors.actor(6));
} else {
   condition = false;
}
</Custom Passive Condition>

Code:
<Custom Passive Condition>
let member1PresentC = $gameActors._data[3] && $gameActors.actor(3).index() < 4;
let member2PresentC = $gameActors._data[4] && $gameActors.actor(4).index() < 4;
let member3PresentC = $gameActors._data[5] && $gameActors.actor(5).index() < 4;
let member4PresentC = $gameActors._data[10] && $gameActors.actor(10).index() < 4;
if ((member1PresentC && member2PresentC) && (member3PresentC && member4PresentC)) {
   condition = (!a.isActor() || $gameActors._data[a.actorId()]) &&
    $gameParty.battleMembers().contains($gameActors.actor(3)) &&
    $gameParty.battleMembers().contains($gameActors.actor(4)) &&
    $gameParty.battleMembers().contains($gameActors.actor(5)) &&
    $gameParty.battleMembers().contains($gameActors.actor(10));
} else {
   condition = false;
}
</Custom Passive Condition>

These two states work just fine, even in conjunction with each other they only lag the party switching just a little bit.
But obviously I still get nearly 1000 errors despite attempting to fix it. I'm going to try and keep toggling/ reinstalling my other plugins to see if it fixes anything, or maybe the script just needs some tidying up, I don't know. But thank you helping anyway of course.
The errors I get are constant PASSIVE STATE CUSTOM CONDITION ERROR, Maximum call stack size exceeded, and a million <failed to convert exception to string>
The only scripts appearing in the console are the two extra ones I added, when I delete them I don't see any scripts in the error console, so I'm assuming that the scripts I edited probably need to be cleaned up to stop at least some of the errors.
That shouldn't be an issue if the OP is using the code I provided in post #2, because the members() method, when used in battle, automatically refers to battleMembers.
This should actually work, although you'd probably need to use allMembers() here (otherwise the index is always < 4 during battle)

There must be some kind of hinky interplay going on because there are errors in the above screenshot about parameters.
My guess was that initEquips calls refresh, which evaluates the passive conditions, which causes the infinite loop. When the stack overflows the plugin prevents the error from going through and instead continues with the current refresh, which then tries to check the actors mhp and mmp parameters with a nearly maxed out stack.

Okay getting somewhere now!
The above code does set the two chosen actors to have a passive state only when they are in the first four slots of the party.
The code above was intended to be an alternative to the previous code in case that previous code caused the errors. You should be able to reduce your last condition to this:
Code:
<Custom Passive Condition>
  condition = (!a.isActor() || $gameActors._data[a.actorId()]) &&
    $gameParty.battleMembers().contains($gameActors.actor(3)) &&
    $gameParty.battleMembers().contains($gameActors.actor(4)) &&
    $gameParty.battleMembers().contains($gameActors.actor(5)) &&
    $gameParty.battleMembers().contains($gameActors.actor(10));
</Custom Passive Condition>

or shorter
Code:
<Custom Passive Condition>
  condition = (!a.isActor() || $gameActors._data[a.actorId()]) &&
    [3, 4, 5, 10].every(id => $gameParty.battleMembers().contains($gameActors.actor(id)));
</Custom Passive Condition>

*If you prefer going with the index method you can of course replace
$gameParty.battleMembers().contains(...)
with
$gameParty.allMembers().indexOf($gameActors.actor(5)) < 4
This should actually work, although you'd probably need to use allMembers() here (otherwise the index is always < 4 during battle)
No it's not? I'm not sure what you're getting at here...if the battle party is the default size of 4, and the looked at actors are within those first 4, then they will have the exact same index inside of allMembers as they do inside of members (which is the same as battleParty while in battle).

If they're not, then they're not going to get a value returned by members().indexOf() while in battle.

Hell, if you think the actor() method is what's causing the problem, simply don't use it.

Code:
condition = $gameParty.members().filter((actor, index) => {(actor.actorId()==5 || actor.actorId()==6) && index<4}).length==2;

No need for anything more complicated than that. But with the weird stuff in the console above, I don't think that's the problem.
If they're not, then they're not going to get a value returned by members().indexOf() while in battle.
If they are not, then members().indexOf() returns -1, which is smaller than 4. At least from my understanding that seemed to be one of the problems curanaca encountered.

Edit: To clarify, this is merely a possible explanation I could come up with, but it would make sense to me.
If they are not, then members().indexOf() returns -1, which is smaller than 4.
Oh that's correct, I thought it returned undefined. Still, see my edit above.

Know what? Let's put this to bed the thirty-second way: I pasted the code I provided in Post #2 into a state in a new project.

It works perfectly as expected, in and out of battle. For that matter, the OP's original code works perfectly.

The custom condition is not the issue.
I'm really at a loss, no matter what I do I just can't get it to work without flooding me with errors. If it's client side I've tried reinstalling rpg maker and the plugins, I have 1.6.3 and I obviously got it and all the plugins legally. Tried it on several new projects and still nothing. Rpg maker is on my c drive along with every project folder as well. Is there just something simple I'm missing? Like I'm just not making the state the right way or something? At this point I'd even be happy to just be called an idiot for missing something simple. I really just want to know what's going wrong here ;_;
Did you do what I did? Make a new project, put only Auto Passive States in it, set up a passive state, give it that condition.

It should work. If no, try redownloading your Yanfly archive, it has happened that people get their files corrupted.

If that works, then go back to your main project and turn off everything except Auto Passive States. See if that works.

Go from there.
The first two scripts actually do work if you didn't want the two actors present in the code to actually have the passive state, because when you do give those two actors the passive state the whole system blows up, using the code I got from my other post above fixes this and works fine. Until I add the partySystem plugin then it all falls apart again (which is necessary to have for the party switching plugin.)

Actually, it only stops working when the two actors present in the custom condition are given the passive state, so if actors 5 and 6 are present in the code, putting the passive state on actors 3 and 4 works perfectly. Even when you give actors 3,4, and 5 the custom state it still works yet when you give actors 5 AND 6 the state then the whole thing gives out.

This was all in a new project and my main one. It only becomes an issue when the two actors that are supposed to have the passive state actually HAVE that passive state which wouldn't make much sense (and I have the partySystem plugin installed.) Plus, actually putting parameters on the state after this only increases the amounts of errors received.
I redownloaded the plugins as well incase they were actually corrupted.
Then just don't do it that way.

Turn on or off switches when you add/remove those actors from the party and make the state reliant on the switches.

I can't think of why putting the state on the actor it's checking would cause an infinite loop, but if you're saying you can reliably reproduce that, just change how you're doing it.

Original thread: https://forums.rpgmakerweb.com/threads/help-with-passive-states-maximum-call-stack-size-exceeded-mv.162177/