MV Counting size of the party

Hi Everyone,

I'm am just getting into development with RMMV and despite having some background with development in other languages, I'm finding some aspects of Javascript difficult to wrap my head around. I've been struggling with a problem that seems like it should be a relatively simple to solve for, but I'm at an impasse, possibly because I'm not understanding fundamental about the language, but also probably because I can't understand the syntax of what's being done. I'm hoping someone might be willing to give me some guidance.

I am working on some code that will trigger during battles where a random party member might say a random message for a bit of spontaneous color during battles. The party leader remains silent, so the very first check I want to undertake is what is the number of actors in the party. Reviewing the out-of-the box code, it seems like it should be simple, based on the code I've written the following function:

Game_Party.prototype.partyLargerThanHero = function() ​
{​
return this.size() > 1;​
}​

However, when I call it, it returns a TypeError of "Cannot read property 'map' of undefined". Following the Game_Party.prototype.size function, it calls Game_Party.prototype.members, which in turn calls Game_Party.prototype.allMembers, which is where the dev tools indicate we are having the problem. So, that far I seem to be okay with what's going, but when it comes time to evaluate allMembers, I'm feeling less confident, specifically the portion in red:

Game_Party.prototype.allMembers = function() {​
return this._actors.map(function(id) {​
return $gameActors.actor(id);​
});​
};​

What I interpret this as being intended to do is going through the list of actors in the Game_Party and returning an array of those who are currently in the party, but the .map command in JavaScript is a new concept to me. From what I can understand of more general documentation is that it needs to be called on an array and this error suggests that I've passed it an empty one and I'm afraid I don't know my tool belt well enough to check that.

So, in so many words (A lot, in fact), here are my questions:

- How would I check the array being used in the allMembers function?
- Are there any thoughts on why I would be passing an empty array in the first place? It all seems to be out-of-the-box code, so I would think it would work on the Game_Party object which I thought would be initialized.
- Lastly, am I totally off course and there's an obvious way to check the party size that I'm missing?

Wow... That was a lot. Apologies for going on like that. If anyone has any thoughts they would be willing to share, I would really appreciate it. Thanks 😀
Alright, after some trial-and-error, I see that I was approaching the initial problem incorrectly, due to my ignorance of the structure of object data. For those who might be facing a similar problem, here is how I was able to, at least, evaluate the size of the party:

Game_Party.prototype.partyLargerThanHero = function() {​
return $gameParty._actors.length > 1;​
};​

By accessing the global variable $gameParty I was able to get at the length of the _actors data, which is an array that I was able to count.

I'm still not entirely sure why I wasn't able to call the data from Game_Party, so if someone could educate me on what I did wrong I would definitely appreciate the insight.
Your approach looks OK to me! Array.map is a shorthand for "generate a new array from a given one, where each element is equal to a given function of its corresponding original element". Here it's swapping actor IDs for Game_Actor instances. More details~
Your solution for checking party size is more elegant if you are not using any plugins that hook into the members method (e.g. NPC followers?). 🙂

this.size() should work from a Game_Party context. The error, as you correctly surmised, means that the _actors array is not defined. My guess is that you may have been calling your method before that array was initialised?
Spoiler: Game_Party > initialize
JavaScript:
Game_Party.prototype.initialize = function() {
    Game_Unit.prototype.initialize.call(this);
    this._gold = 0;
    this._steps = 0;
    this._lastItem = new Game_Item();
    this._menuActorId = 0;
    this._targetActorId = 0;
    this._actors = [];   // <- here!
    this.initAllItems();
};
I'm using the past tense because length is not a property of undefined either, so I'd expect the same kind of error. 😅

Note that if you ever want to check party size via event commands, you can use:
  • Control Variables > Game Data > Other > Party Members
@caethyril

This is great information, thank you so much time for taking the time to reply and even more thanks for the examples. I think I'm getting a better handle on things one step at a time 😀

Original thread: https://forums.rpgmakerweb.com/threads/counting-size-of-the-party.125878/