I have a script snippet that loops through $dataActors and builds an array of all actors who are not currently in the party.
Each $dataActor element is an Object, so I convert it into a Game_Actor object in order to do the comparision and add to the array. But when it's all done, the array is full of elements of type Object, not Game_Actor. Can't figure out what I'm doing wrong.
Game_Party.prototype.getNonPartyActors = function() {
var tempActors = $dataActors.filter(function(actor) {
if (!actor) return null;
// at this point, actor exists and is of type Object
actor = $gameActors.actor(actor.id);
// now actor is type Game_Actor
if ($gameParty.members().indexOf(actor) < 0) {
return actor;
// actor is still a Game_Actor here
}
}.bind(this));
if (tempActors.length > 0) {
// now tempActors is filled with elements of type Object, not Game_Actor
...
}
};
I wondered if it was because I was using the same variable name, so I changed it to use two different variables for the $dataActors object and the $gameActors object. Still get the same results.
Edit: okay - it's because I'm using filter I think. Changed it to map instead, but now it's including null elements. I'm just going to change it to a regular forEach loop and be done with it, as I'm not sure there's a way to do it like I want, unless I do a filter AND a map.
Each $dataActor element is an Object, so I convert it into a Game_Actor object in order to do the comparision and add to the array. But when it's all done, the array is full of elements of type Object, not Game_Actor. Can't figure out what I'm doing wrong.
Game_Party.prototype.getNonPartyActors = function() {
var tempActors = $dataActors.filter(function(actor) {
if (!actor) return null;
// at this point, actor exists and is of type Object
actor = $gameActors.actor(actor.id);
// now actor is type Game_Actor
if ($gameParty.members().indexOf(actor) < 0) {
return actor;
// actor is still a Game_Actor here
}
}.bind(this));
if (tempActors.length > 0) {
// now tempActors is filled with elements of type Object, not Game_Actor
...
}
};
I wondered if it was because I was using the same variable name, so I changed it to use two different variables for the $dataActors object and the $gameActors object. Still get the same results.
Edit: okay - it's because I'm using filter I think. Changed it to map instead, but now it's including null elements. I'm just going to change it to a regular forEach loop and be done with it, as I'm not sure there's a way to do it like I want, unless I do a filter AND a map.
Last edited by a moderator:
