Accessing the right actorId

I'm using Yanfly's Auto-Passive States to create passive states that activate when an actor wears a certain number of equipment of a single armor type. Preferably, I would like the state to take effect immediately when placing the necessary amount of armor on the actor. The code at this moment is below.

<Custom Passive Condtition>
let typeMatch = 0;

for (let i = 1; i < 4; i++) {
if (user.equips()[i].atypeId === 1) {
typeMatch += 1;
}
}

if (typeMatch < 2) {
condition = false;
} else {
condition = true;
}
</Custom Passive Condition>

Looking in the console, I keep getting "TypeError: Cannot read property 'atypeId' of null", which means that user is undefined. Based on other threads I've read, it would seem that user isn't defined until battle is initiated.

I think the script call that I should be using instead would be $gameActors.actor(x).equips()[i].atypeId, where x is the relevant Actor's ID. Is there a script call or set of script calls that would allow me to access the relevant Actor's ID from the note tag? Or am I going about this all wrong?
I keep getting "TypeError: Cannot read property 'atypeId' of null", which means that user is undefined.
That's not correct. The atypeId isn't a property of the user, it's a property of the item you're looking at (e.g. user.equips()[i] ). If user were null, you'd be getting the error "Cannot read property equips of null".

Based on other threads I've read, it would seem that user isn't defined until battle is initiated.
What threads? That's also not correct.

The function that evaluates the code in the notetag is a method of Game_BattlerBase, and user is defined as the instance the state is attached to. That has nothing to do with being in battle or not.

Or am I going about this all wrong?
Yes. You're assuming that the actor is equipping exactly 4 pieces of equipment and you're only checking 3 of them for some reason.

for (let i = 1; i < 4; i++)
That's going to check 1, 2, and 3 - whether or not the actor is wearing that many items and ignoring the first item, which would be index 0.

Instead, try this:
Code:
user.equips().forEach(equip => {if (equip && equip.atypeId == 1) typeMatch++; });

Typed out as a for loop, that would be
Code:
for (let i = 0; i < user.equips().length; i++)

That's starting at index 0, so you'll look at everything, and it's going off of the calculated length of the equips array, so you won't go further than they actually have and try to look at null entries.

Just as a little typing efficiency tip: the chunk at the bottom of your notetag is fine, it's functional. But
1 - you don't need braces after conditionals that have just one statement
2 - if you're doing something as the result of a condition and it's only setting something to true or false, then the whole condition is unnecessary.

You could just do:
Code:
condition = typeMatch > 1;
That's not correct. The atypeId isn't a property of the user, it's a property of the item you're looking at (e.g. user.equips()[i] ). If user were null, you'd be getting the error "Cannot read property equips of null".


What threads? That's also not correct.

The function that evaluates the code in the notetag is a method of Game_BattlerBase, and user is defined as the instance the state is attached to. That has nothing to do with being in battle or not.


Yes. You're assuming that the actor is equipping exactly 4 pieces of equipment and you're only checking 3 of them for some reason.


That's going to check 1, 2, and 3 - whether or not the actor is wearing that many items and ignoring the first item, which would be index 0.

Instead, try this:
Code:
user.equips().forEach(equip => {if (equip && equip.atypeId == 1) typeMatch++; });

Typed out as a for loop, that would be
Code:
for (let i = 0; i < user.equips().length; i++)

That's starting at index 0, so you'll look at everything, and it's going off of the calculated length of the equips array, so you won't go further than they actually have and try to look at null entries.

Just as a little typing efficiency tip: the chunk at the bottom of your notetag is fine, it's functional. But
1 - you don't need braces after conditionals that have just one statement
2 - if you're doing something as the result of a condition and it's only setting something to true or false, then the whole condition is unnecessary.

You could just do:
Code:
condition = typeMatch > 1;
It works!!! Thank you!

Original thread: https://forums.rpgmakerweb.com/threads/accessing-the-right-actorid.167000/