This.getNearest(units) returns null


#1

I cannot get nearest unit from custom array, as this.getNearest return null (?)

this.getSoldiersNearBase = function(units){
var arr = [];
for(var i=0;i<units.length;++i){
var u = units[i];
if((u.type===‘soldier’ || u.type===“knight”) && u.pos.x>70) arr.push([u]);

}
return arr;

};
this.defendBase = function(threat){

this.attack(threat); //ArgumentError is not defined at this line;
if(!this.getCooldown('throw')) this.throw(threat);
if(!this.getCooldown('jump'))  this.jumpTo(threat);

};
var enemies = this.getEnemies();
var soldierNearBase = this.getSoldiersNearBase(enemies);
if(soldierNearBase.length > 0){

var threat = this.getNearest(soldierNearBase);
this.say(soldierNearBase); //this simple say is to check, that soldierNerBase really constains units

// at this moment threat does not contain any information.
/if ask “this.say(threat)” compilier returns "say what?"
this.defendBase(threat);
}


#2

Found the problem; it’s that you are pushing [u] instead of u, so you end up with this array of arrays of units instead of an array of units like this.getNearest() is expecting.


#3

Thanks for spotting that typo