MV HUD Maker JS Help

Quick question... I think I'm in the ballpark, but I'm having trouble trying to get a script-call to work with SRD's HUD Maker & Yanfly's Armor Durability. I am trying to get an icon to appear when any piece of equipped armor, that the first actor (or the party leader) is wearing, falls to 20 durability or lower; see the screenshot below.

ScreenShot_20230214201716.png


Currently, I'm using the line below to attempt this, but it isn't working (but it also isn't throwing out an error, so I assume I used the wrong call for the durability check; QED Yanfly's script).
Code:
$gameParty.members()[0].armors().durability <= 20

TIA
armors() returns an array, you'll need a loop. This may work:
JavaScript:
$gameParty.members()[0].armors().some(function(armor) { return armor.durability <= 20; })
armors() returns an array, you'll need a loop. This may work:
JavaScript:
$gameParty.members()[0].armors().some(function(armor) { return armor.durability <= 20; })

That's the ticket! Worked like a charm. Thanks a mil.
(didn't know that "armors()" returned as an array... makes sense tho... thank for mentioning that)
Great! Happy RPG Making~ 👋
Just encountered a minor hiccup with this code request. It definitely works as I had originally intended it, but with "unbreakable" armors it also triggers the icon to appear (b/c "unbreakable" armors have a "-1" value). So, I need a way to make it return no less than 0. I've already tried the methods I'm familiar with........ e.g.
Code:
$gameParty.members()[0].armors().some(function(armor) { Math.min(0, armor.durability <=20); })
//and
<=20 && >=0

But since my JS knowledge is limited, I'm having trouble trying to find the right combination, lol. Help? lol TIA
Try this:
JavaScript:
$gameParty.members()[0].armors().some(function(armor) { return armor.durability >= 0 && armor.durability <= 20; })
I.e. "does any armor equipped on the party leader have durability that is (0 or more) and (20 or less)?".

The return value for the function passed to Array#some is true or false, indicating whether or not this armor meets the condition.
Of course your suggestion works, lol! Man... that is so frustrating. I could have SWORN that I tried arranging it the way you suggested at least once... but after the dozen or so ways I've tried it, it's all a jumble in my head now anyway. Thanks a mil !
No worries, you're welcome! 👋
One more request, if you don't mind. This request is slightly different than this thread's original topic, but it's close enough that it might help someone else looking for the same thing.

Do you have a suggestion as to the script call used to retrieve a string (number) value for the armor that is linked to the icon (does that make sense)? In other words, in addition to the armor icon showing up, it'd be nice to show the players how much durability (as a numeric value) they have left on the HUD instead of them having to open their menu.

*PS As a side note, I typically don't bother anyone with these kinds of questions unless I've tried to figure it out for myself for at least a little while. I don't mind learning the JS I need to get done what I want in my game, but I don't want to dive so deep into JS research that I may as well become a JS scripter, lol. 😛

Edit #2: I just found THIS, & may be able to figure out what I want from a previous thread you helped someone on. So lemme try to figure it out & I'll get back to you if I can't.
In other words, in addition to the armor icon showing up, it'd be nice to show the players how much durability (as a numeric value) they have left on the HUD instead of them having to open their menu.
To get the lowest durability value, you could try this:
JavaScript:
$gameParty.members[0].armors().reduce(function(a, c) {
  var d = c.durability;
  return d < 0 ? a : Math.min(a, d);
}, Infinity)
I've put it on multiple lines for readability but you can remove the line-breaks: doing so won't change how it works.

Array#reduce is another kind of loop: it accumulates a single value, a, by iterating over each element, c, in the given array. In this case it gets the lowest durability value from all breakable armors, or Infinity if everything is unbreakable.

Edit: I'm guessing you could just not display the value if it's over 20, right?
Unfortunately, that returns an error on SRD HUD Maker, so I'm not sure if SRD's plugin can handle that much script, lol.

On a bright note, using the suggestion you made on a previous post (the one I linked above), I was able to extrapolate enough info to make what I want work... the only downside is that since unbreakable armor returns a value of -1, that's what shows up in my HUD text box. I'm currently trying to figure out a way around that.

Here's what I have currently:
  • A "parallel" global event that is triggered on game start (it runs once then self-switches to keep it from looping)...
  • Among other switches & things activated in this "Game Start" event is the following script:
JavaScript:
var item = $gameParty.members()[0].equips()[3];
var varId = 988;
var value = DataManager.getDurability(item);
$gameVariables.setValue(varId, value);

Inside of SRD's HUD maker I created a text box with the following value:
Code:
${$gameVariables.value(988)}

This method seems sloppy (e.g. more steps than needed) to me, but it works... unless you have an alternate idea. But as I mentioned earlier, doing it this way will return a value of "-1" when unbreakable armor is worn... so I'm in the process of trying to figure out how to "zero out" the -1 value or change it to something like a string that says "Unbreakable" (that would be pref).

**Edit... I stand corrected... it does NOT work. LOL *Sigh*
Back to the drawing board on this process I guess, lol
If you just tried to paste it into a single-line textbox as-is, then it will have been cut off, causing a syntax error. This is the single-line version:

$gameParty.members[0].armors().reduce(function(a, c) { var d = c.durability; return d < 0 ? a : Math.min(a, d); }, Infinity)
Should be A-OK...in terms of length, it's only 10 characters more than the previous one.

However, I'm not clear on what your goal is here. I originally assumed you had 1 icon as a "low armor durability" warning kind of thing, and I've been running on that assumption throughout, hence the loops. But if you have, for example, 1 icon per armor equip slot, then you can just fetch the values directly, e.g.
  • Get item equipped in slot 1 (equip type 2) for party member 0 (leader):
    JavaScript:
    var m = $gameParty.members()[0]; m ? m.equips()[1] : null
  • Get that item's durability value for display purposes (blank if item slot empty):
    JavaScript:
    var m = $gameParty.members()[0]; var o = m ? m.equips()[1] : null; o ? (d < 0 ? "Unbreakable" : d) : ""
    😴 Edit: whoops, I forgot to define the d variable for this one. Try this instead:
    JavaScript:
    var m = $gameParty.members()[0]; var o = m ? m.equips()[1] : null; o ? (o.durability < 0 ? "Unbreakable" : o.durability) : ""
  • Is that item at 20 or less durability?
    JavaScript:
    var m = $gameParty.members()[0]; var o = m ? m.equips()[1] : null; !!o && o.durability <= 20
I've included checks in all of these to handle cases where the party slot or equip slot is empty.
One more request, if you don't mind. This request is slightly different than this thread's original topic, but it's close enough that it might help someone else looking for the same thing.
No... you are correct. The original question I had for this thread WAS for just an icon (which works beautifully, btw).

I just realized, however, that I have drastically over-complicated what I'm trying to do, & have likely irritated you in trying to help me. I'm very sorry. I will do that (over-complicate things) when I spend too much time on them. After stepping away for a while, I realized my mistake. I've since added my question to a new thread... & as promised, have edited this one to say that my original question was answered (thanks again), & I've added my additional request to a new thread: HERE (for any interested).

Thanks a mil @caethyril, you've been a huge help (incl. helping me learn a little more about JS by reverse engineering your suggestions), & sorry for the confusion.
No worries! I shouldn't've made that assumption in the first place, so you can consider us even. 😛

Original thread: https://forums.rpgmakerweb.com/threads/hud-maker-js-help.154976/