tower.attack is not a function...???
-
// Handle repairing and attacking for towers
var towers = _.filter(Game.structures, (s) => STRUCTURE_TOWER);
for (let tower of towers) {
var closestWounded = tower.pos.findClosestByRange(FIND_MY_CREEPS, {
filter: (w) => w.hits < w.hitsMax
});
var closestHostile = tower.pos.findClosestByRange(FIND_HOSTILE_CREEPS);
var closestDamagedStructure = tower.pos.findClosestByRange(FIND_STRUCTURES, {
filter: (s) => s.hits < s.hitsMax && s.structureType != STRUCTURE_WALL
});
if(closestHostile) {
tower.attack(closestHostile);
}
if ((closestWounded) && (tower.energy > 250)) {
tower.heal(closestWounded);
}
/*if ((closestDamagedStructure) && (tower.energy > 990)) {
tower.repair(closestDamagedStructure);
}*/
}
This is all my code related to towers, I'm stumped as to why I keep getting this "TypeError: tower.attack is not a function
at Object.module.exports.loop (main:74:8)
at __mainLoop:1:52"
In my emails whenever an npc creep comes and wipes out my colony (well it wouldn't if my tower was working)
tower.repair() works just fine, so I can't understand why this error is showing.Any help would be greatly appreciated!
PS: line 74 is tower.attack(closestHostile);
PPS: don't mind the commented out repair section, still working on making it periodic
-
first step of any screeps debugging: console.log!
right above tower.attack(closestHostile), do this:console.log(tower);
console.log(JSON.stringify(tower));
console.log(closestHostile);
console.log(JSON.stringify(closestHostile);
this will help illustrate if your variables contain what you think they should contain at that point.
-
Ah good point! Next time that will be the first thing I do haha
when I console logged tower, it returned every structure I have, then I noticed var towers = _.filter(Game.structures, (s) => STRUCTURE_TOWER); was missing something.
Changed it to this:
var towers = _.filter(Game.structures, (s) => s.structureType == STRUCTURE_TOWER);
and now everything works like a charm!
Thanks!