Structures not returning as objects
Closed this issue · 1 comments
When I run code to search for structures through Game and Rooms I'm not able to access their methods as I would expect.
To use your tower script as an example:
var towers = _.filter(Game.structures, (t) => t.structureType == STRUCTURE_TOWER);
console.log(towers) // array of objects as expected
console.log(towers[0]) // an object as expected
for (const tower in towers) {
console.log(tower); // Returns "0". What is this data type? It should be the object but isn't?
var target = tower.pos.findClosestByRange(FIND_HOSTILE_CREEPS);
// if one is found...
if (target != undefined) {
// ...FIRE!
tower.attack(target);
}
}
tower.pos
and tower.attack
both return as undefined.
You can view my screeps repository for another example in the StructureManager.js script.
I'm guessing there has been some change to how javascript handles for loops. It appears the variable tower
above was pointing to the index.
The code below fixes this issue:
var towers = _.filter(Game.structures, (t) => t.structureType == STRUCTURE_TOWER);
// console.log(towers)
// console.log(towers[0]);
for (const key in towers) {
let tower = towers[key];
console.log(tower);
var target = tower.pos.findClosestByRange(FIND_HOSTILE_CREEPS);
// if one is found...
if (target != undefined) {
// ...FIRE!
tower.attack(target);
}
}
Closing issue for now, although I do not know why the projects code has stopped working.