Check if actor has equipment type equipped

As the title says, I'm trying to figure out and see how to check if an actor has an equipment type equipped. I've been using the script call
Code:
$gameActors.actor(actor_id).equips()[slot_id] == null;
but either I'm not using it right or it's not what i'm looking for. The actual code I'm using in my conditional branch is
Code:
$gameActors.actor(1).equips()[2] == 14;
"2" is the equipment type and "14" is the armor type. I don't really need to check the equipment type, the armor type is what's important.
Can you open the console (press F8 during playtest to open it) and paste the following and press enter on your keyboard? What comes up?

Code:
$gameActors.actor(1).equips()[2]
Code:
$gameActors.actor(1).equips()
will return an array of equipment objects, not just the ID. You can use wTypeId for Weapon type ID and aTypeId for Armor type ID.
So your code will be something like this:
Code:
$gameActors.actor(1).equips()[2] && $gameActors.actor(1).equips()[2].aTypeId == 14
Actor 1 slot 3 equipment is not empty and Actor 1 equipment slot 3 armor type ID is 14
Edit:
I forgot that what you've asked is the equipment type. For getting the equipment type ID use eTypeId.
@Aloe Guvner It returns "null"

@DrDhoom I tried
Code:
$gameActors.actor(1).equips()[2] && $gameActors.actor(1).equips()[2].aTypeId == 14
but it didn't work either.
I've edited my post, basically change aTypeId to eTypeId.
But if it's null then either you didn't set the equipment correctly or the slot index is wrong. Which equipment slot is the equipment that you want to check? Remember that the index start from 0.
@DrDhoom Under equipment types it's set as 2, so I'm guessing I need to change the 2 to a 1. The armor type is also set as 14 so should I change that to 13?
No, only for the index, you can leave the armor type ID as it is.
Screenshot_120.png
@DrDhoom Hmm I removed Head and Shield so the Body/Armor ID is 1 for me. Script still isn't working though. I set it up as...
Code:
$gameActors.actor(1).equips()[1] && $gameActors.actor(1).equips()[1].eTypeId == 14
Assuming I get it working, is the first part of the code necessary?
Change eTypeId to aTypeId, you want to check the armor type ID right? Sorry if I confused you xD

Assuming I get it working, is the first part of the code necessary?
Yes, without it the game will crash if the equipment slot is null/empty.
Yes, without it the game will crash if the equipment slot is null/empty.
Yeah, this is an annoying part of Javascript. There's a proposal to add an operator for that which we might see in 2019.
@DrDhoom Ah, ok, was just checking.

Code:
$gameActors.actor(1).equips()[1] && $gameActors.actor(1).equips()[1].aTypeId == 14
still isn't working though. I typed it in the console (f8) to see what it would say and when I do it returns "false". I'm pretty sure the script is working, or at least doing something, it' just not looking for what I want it to look for. Just so I completely understand the basics of what's going on here...

$gameActors.actor(1)= The ID for which game actor you want referenced, sorted by the actors list in the actor panel.
equips()[1]= The ID for which equipment type you want to reference, sorted by the actors initial equipment type in the actor panel.
&&= Adds another condition.
$gameActors.actor(1)= Back to which actor ID to reference, should be the same as the first reference.
equips()[1]= Back to the equipment type ID to reference, should be the same as the first reference.
aTypeId == 14= The ID for the armor type referenced based off the armor types in the types panel.
Dang it, the script is wrong. My bad xD
It is atypeId instead of aTypeId.

$gameActors.actor(1)= The ID for which game actor you want referenced, sorted by the actors list in the actor panel.
equips()[1]= The ID for which equipment type you want to reference, sorted by the actors initial equipment type in the actor panel.
&&= Adds another condition.
$gameActors.actor(1)= Back to which actor ID to reference, should be the same as the first reference.
equips()[1]= Back to the equipment type ID to reference, should be the same as the first reference.
aTypeId == 14= The ID for the armor type referenced based off the armor types in the types panel.
Ding ding!
@DrDhoom Nice, changing it to atypeId worked like a charm! Script is running fine now, thanks for your help!
For the future, the console is really helpful for things like this. You can go through each part step-by-step to understand what it's doing:

upload_2018-12-30_13-55-16.png


And to explain why you need the first part of this (the part before the &&)
Code:
$gameActors.actor(1).equips()[1] && $gameActors.actor(1).equips()[1].atypeId == 14
&& means "and" in Javascript. If you have a condition that doesn't have things like "==", ">=", "<", etc. then what that condition is doing is figuring out whether it exists or not. So the first part of it:
Code:
$gameActors.actor(1).equips()[1]
This is just checking whether the 2nd equip exists or not. If the 2nd equip exists then it moves to the next part. But if the 2nd equip doesn't exist, it will return false and it won't try the next part.

If you didn't have the first part, and used only this:
Code:
$gameActors.actor(1).equips()[1].atypeId == 14
If the actor didn't have a 2nd equip (the 2nd equip doesn't exist), then it would basically trying to do this:
Code:
undefined.atypeId == 14
And you would receive an error like this that crashes the game:
  • Uncaught TypeError: Cannot read property 'atypeId' of undefined


Also, things start counting at zero, which is why
  • $gameActors.actor(1).equips()[0] --> the first equip
  • $gameActors.actor(1).equips()[1] --> the second equip
  • etc.
Is there a way to do this, or something similar to this, in a damage formula? Rather than start a new post I figured I'd ask here. I was looking over at a nice damage formula thread and it seems to be possible but I'm unsure of the syntax.

I'm pretty sure "actorId()" is the relevant variable I'm looking for, but how to use it I'm not so clear on. I'm using
Code:
if actorId(1) 50 + (v[307] * 2); else 0
as my damage formula but it crashes the game. The "else 0" is there, potentially wrongly, because it's an if then statement but I mainly just need to get the first part to work. The formula is meant to do fixed damage, ignoring defense, plus an actor unique multiplayer times 2. Once the code works as intended I'll add additional actors to the formula. The end result would look something like this if it was proper formula, which it's not.
Code:
if actorId(1) 50 + (v[307] * 2) || actorId(2) 50 + (v[308] * 2) || actorId(3) 50 + (v[309] * 2); else 0
The last part isn't needed in a working formula, unless it's actually needed for said formula to work.

Is what I want possible?
It's kind of a different question, but here's some general advice:
  1. It's always possible, it's just a matter of getting the syntax correct. The damage formula is Javascript, so you can always look up Javascript syntax
  2. In the damage formula 'a' refers to the user of the skill and 'b' refers to the target of the skill. Take a look at the default formula for 'Attack'. So you'd have the check the actor ID of 'a' which is the user
  3. If the skill can also be used by enemies, then you'd have to check to make sure that 'a' is an actor. Because enemies don't have an actor ID
  4. Depending on how many actors you have, using 'if...else' becomes unmanageable. There are other ways such as 'switch...case', using a key/value map, or numbering your variables smartly
A great damage formula guide:
https://steamcommunity.com/sharedfiles/filedetails/?id=681253029

Javascript if...else
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/if...else

Javascript switch...case
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/switch

Javascript object literal (key/value map)
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Object_initializer
I forgot the "a". I got
Code:
if (a.actorId(1)) 50 + (v[307] * 2); else 0
to work but it always returns true. I could change the 1 to 10 and not have actor 10 in the party but it would still do damage and not 0
actorId() returns the actor's id if I recall correctly. It's not a boolean and therefore you have to compare the value to a number.
Code:
if (a.actorId() === 1) 50 + (v[307] * 2); else 0
Does this work or am I talking out of my hat? I cannot check if it does right now.
@ozubon I must've been tired because I should've seen that. I got it to work using the syntax
Code:
if (a.actorId() ===1) 50 + (v[307] * 2); else if (a.actorId() ===2) 50 + (v[307] * 2); else 0
I only need it to work for one actor right now but I wanted to make sure I could set it up to work for at least two. Thanks for your help!

Original thread: https://forums.rpgmakerweb.com/threads/check-if-actor-has-equipment-type-equipped.103836/