How to get array of Actor's Armors?

So here's what I'm trying to do.

 

I want to create some Notetags in the Armor Database that say

<applesGained: x>

<applesLost: x>

 

Then, I want to add them all up based on which ones the specific Actor is wearing, so I created this function:

 


Code:
var processDemArmors = function(actor){    var total = 0;     for(i = 0; i < actor.equips().length; i += 1)    {        if(actor.equips()[i].meta.applesGained)        {            total += Math.floor(actor.equips()[i].meta.applesGained);        }        if(actor.equips()[i].meta.applesLost)        {            total -= Math.floor(actor.equips()[i].meta.applesLost);        }    }     return total;}

 

But I keep getting an error that states:

 

TypeError

Cannot read property 'meta' of null

 

Then only thing I can think of is that I'm not properly accessing the Armors...

 

Can someone explain what I'm doing wrong?
Do your actor have all equips? Since empty equipment slot will return null.

Try this

Code:
var processDemArmors = function(actor){    var total = 0;    for(i = 0; i < actor.equips().length; i += 1)    {        if (actor.equips()[i]) { // check if actor equip slot isn't null            if(actor.equips()[i].meta.applesGained)            {                total += Math.floor(actor.equips()[i].meta.applesGained);            }            if(actor.equips()[i].meta.applesLost)            {                total -= Math.floor(actor.equips()[i].meta.applesLost);            }        }    }     return total;}
Do your actor have all equips? Since empty equipment slot will return null.

Try this

var processDemArmors = function(actor){    var total = 0;    for(i = 0; i < actor.equips().length; i += 1)    { if (actor.equips()) { // check if actor equip slot isn't null        if(actor.equips().meta.applesGained)        {            total += Math.floor(actor.equips().meta.applesGained);        }        if(actor.equips().meta.applesLost)        {            total -= Math.floor(actor.equips().meta.applesLost);        } }    }     return total;}


Thanks for the feedback. 

The game no longer crashes, but a problem still exists...

Now, it appears as if the function only returns 0 unless the Actor has a weapon equipped.

Does anyone have any explanations for this?
That's weird. That kind of check should work.

By the way, as far as I know, meta will always return a string. Maybe you should change it to number.
@Heartbreak61

I'm not sure if that's correct.

I've never had trouble with the .meta not returning a number.

I surrounded the values with Number(), it still didn't work...

var processDemArmors = function(actor){    var total = 0;    for(i = 0; i < actor.equips().length; i += 1)    {if (actor.equips()) { // check if actor equip slot isn't null        if(actor.equips().meta.applesGained)        {            total += Math.floor(Number(actor.equips().meta.applesGained));        }        if(actor.equips().meta.applesLost)        {            total -= Math.floor(Number(actor.equips().meta.applesLost));        }}    }     return total;}EDIT:

So I figured it out.

It turns out I didn't save my file when I went onto my browser to ask this question, so some of the calls were done with

actor.armors()instead of

actor.equips()
I switched these around when I was looking for the solution.

It turns out your solution was the correct one from the beginning, @Heartbreak61.

Thanks for your help!
Glad I could help.

I learned a new thing too that Math.floor will convert string to number 😀
Have you check $gameActors. they should have a key for weapons or armor or equipped which lets you get the id's of whats equipped, if not the object 😀

Original thread: https://forums.rpgmakerweb.com/threads/how-to-get-array-of-actors-armors.50628/