How to check "IF all active party members are wearing Armor N"?
Equipment check for all active actors
Learning JavascriptWhat do you mean with active? Only the first 4 even if there are 8 or 9?
Or the first 4 Members in Party maybe even being alive?
Is this for Map Stuff or for the Battle Scene or for both?
Was the other Thread not about the same Question?
Did we do something wrong? Were we missing something?
Help us to help you. =)
Or the first 4 Members in Party maybe even being alive?
Is this for Map Stuff or for the Battle Scene or for both?
Was the other Thread not about the same Question?
Did we do something wrong? Were we missing something?
Help us to help you. =)
Of course, alive. Dead people doesn't wear armor.
For Map AND for Battle Scene! Because AIRC states and equipment that weared and affected during battle, can remains in Map. And vice versa.
The sutiation is next:
-I have 9 characters in the database. It means that I got some troubles with checking equipment in conditional branch;
-Party is always changing! Because characters joins and leaves the party during the storyline;
-I want to make events which activate only if all party members wearing specific armor;
-I know how to make events which activates when all entire party is affected by specific state.
Edit: I totaly oversaw that this is in the Java Script Section. Iam not a Scripter, so iam wrong here. Sorry =)
AIRC active member are the first 4 characters in party formation.
If party contains more than 4, then the other ones stays as reserve.
I need script for conditional branch, which allows to check equipment on all characters in party. At least if this party contains only 4 active characters.
Since this is a Learning JavaScript board, you can look through the explanation in the spoiler below for how to find if everyone in the party is wearing something:
To make it fit in one line for your script call in your conditional branch:
If you happen to be using RMMV 1.6 or later, you could shorten it up a bit more with arrow functions:
All you have to do is replace the number 4 with whatever the Id is of the armor you want to look for.
$gameParty.members() will give you everyone in your party.
$gameParty.aliveMembers() will give you everyone in your party that's alive.
$gameParty.battleMembers() will give you everyone that fights in battle (by default, the first 4 in your formation), dead or alive.
It sounds like you want to reference the $gameParty.aliveMembers() Array, so we'll start there. Each member has an _equips property which is an Array of all the items they're currently wearing, which are instances of the Game_Item constructor. They only have two properties: _dataClass, and _itemId.
Since you want to see if any one of those Game_Items is an armor with a particular Id, you can make use of the Array.prototype.some method, which returns true if your function passed in returns true for any element in the Array.
If you want to see if every element of an Array meets some criteria, you can use the Array.prototype.every method, which returns true if your function passed in returns true for each element.
I don't know what version of RMMV you're using, so I'll write something that should work in earlier versions. Let's assume the armor you're looking for is Id 4. Let's start by making a function that checks if a given item is the one you want:
What this will do is, if you pass in an item that a member is wearing, return true if it's an armor with Id 4. This is the function we'll pass to our Array.prototype.some method when we're looping over all the equips a member is wearing. So let's write a function for that:
So this is a function that accepts one of your party members, then loops through all their equipped items and returns true if one of them is the expected armor. Well, we need to make sure that this is true for every party member, so that's the function we'll pass to our Array.prototype.every method:
We could rewrite this using anonymous functions like so:
$gameParty.aliveMembers() will give you everyone in your party that's alive.
$gameParty.battleMembers() will give you everyone that fights in battle (by default, the first 4 in your formation), dead or alive.
It sounds like you want to reference the $gameParty.aliveMembers() Array, so we'll start there. Each member has an _equips property which is an Array of all the items they're currently wearing, which are instances of the Game_Item constructor. They only have two properties: _dataClass, and _itemId.
Since you want to see if any one of those Game_Items is an armor with a particular Id, you can make use of the Array.prototype.some method, which returns true if your function passed in returns true for any element in the Array.
If you want to see if every element of an Array meets some criteria, you can use the Array.prototype.every method, which returns true if your function passed in returns true for each element.
I don't know what version of RMMV you're using, so I'll write something that should work in earlier versions. Let's assume the armor you're looking for is Id 4. Let's start by making a function that checks if a given item is the one you want:
Code:
function isExpectedArmor(equip) {
return equip._dataClass === 'armor' && equip._itemId === 4;
}
What this will do is, if you pass in an item that a member is wearing, return true if it's an armor with Id 4. This is the function we'll pass to our Array.prototype.some method when we're looping over all the equips a member is wearing. So let's write a function for that:
Code:
function hasExpectedArmor(member) {
return member._equips.some(isExpectedArmor);
}
So this is a function that accepts one of your party members, then loops through all their equipped items and returns true if one of them is the expected armor. Well, we need to make sure that this is true for every party member, so that's the function we'll pass to our Array.prototype.every method:
Code:
$gameParty.aliveMembers().every(hasExpectedArmor);
We could rewrite this using anonymous functions like so:
Code:
$gameParty.aliveMembers().every(function(member) {
return member._equips.some(function(equip) {
return equip._dataClass === 'armor' && equip._itemId === 4;
});
});
To make it fit in one line for your script call in your conditional branch:
Code:
$gameParty.aliveMembers().every(function(member) { return member._equips.some(function(equip) { return equip._dataClass === 'armor' && equip._itemId === 4; }); });
If you happen to be using RMMV 1.6 or later, you could shorten it up a bit more with arrow functions:
Code:
$gameParty.aliveMembers().every(member => member._equips.some(equip => equip._dataClass === 'armor' && equip._itemId === 4));
All you have to do is replace the number 4 with whatever the Id is of the armor you want to look for.
Zevia, I'm using 1.6.2 version.
Hi there!
@Zevia just a curiosity:
If I want to check another piece of equipament, let's say a Sword, I only have to change the 'armor' to match the name of my type of equipament that I put on the database?
@Eliaquim With the default setup in a new project, the only two dataClasses I see are "weapon" and "armor".
I'm not sure if you can generate additional categories without a Plugin or not, but I would imagine if you want to check for a particular sword, you would use "weapon" for the dataClass instead of "armor" and then use the appropriate Id from the weapons tab.
If you want to check for a specific weapon type, like to see if they're wearing any Sword, you'll probably have to use that itemId to check $dataWeapons.
I'm not sure if you can generate additional categories without a Plugin or not, but I would imagine if you want to check for a particular sword, you would use "weapon" for the dataClass instead of "armor" and then use the appropriate Id from the weapons tab.
If you want to check for a specific weapon type, like to see if they're wearing any Sword, you'll probably have to use that itemId to check $dataWeapons.
@Zevia thanks! I got it! ^^
Original thread: https://forums.rpgmakerweb.com/threads/equipment-check-for-all-active-actors.110519/