Hi, been working on this problem for a while. My creeps do not seem to be able to withdraw from the spawn or containers using the variable withdrawTargets as their target, but if I hard code the name of the spawn 'Spawn1' into the section of the code used for withdrawing, it does work. There are several debug lines in the code which I have used to try to isolate the problem, here are a few things that are definitely NOT causing the problem:
- the code to withdraw from the spawn is running
- withdrawTargets[0] is definitely an object
- withdrawTargets[0] when logged to the console returns the spawn
Essentially, my question is, why does the following work:
if (creep.withdraw(Game.spawns['Spawn1'], RESOURCE_ENERGY) === ERR_NOT_IN_RANGE) {
creep.moveTo(Game.spawns['Spawn1'])
}
But not:
if (creep.withdraw(withdrawTargets[0], RESOURCE_ENERGY) === ERR_NOT_IN_RANGE) {
creep.moveTo(withdrawTargets[0])
}
My full code for roleUpgrader is below in case something else is causing the problem:
var roleUpgrader = {
run: function (creep) {
var withdrawTargets = creep.room.find(FIND_STRUCTURES, {
filter: (structure) => { return (structure.structureType === STRUCTURE_CONTAINER) && (structure.store[RESOURCE_ENERGY] > 0)}})
if (Game.spawns['Spawn1'].memory.timeSinceLastSpawn > 50) {
withdrawTargets.push(creep.room.find(FIND_STRUCTURES, {
filter: (structure) => { return (structure.structureType === STRUCTURE_SPAWN) && (structure.energy > 0)}}))
}
if(creep.carry.energy === 0 && creep.memory.upgrading === 'true') {
creep.memory.upgrading = 'false'
}
if(creep.carry.energy === creep.carryCapacity && creep.memory.upgrading === 'false') {
creep.memory.upgrading = 'true'
}
if(creep.memory.upgrading === 'false') {
console.log('code running')
console.log(typeof withdrawTargets[0])
if (creep.withdraw(Game.spawns['Spawn1'], RESOURCE_ENERGY) === ERR_NOT_IN_RANGE) {
creep.moveTo(Game.spawns['Spawn1'])
}
}
else if (creep.memory.upgrading === 'true') {
var repairSites = creep.room.find(FIND_STRUCTURES, { filter: (structure) => { return (structure.hits < structure.hitsMax * 0.75)}})
var buildSites = creep.room.find(FIND_MY_CONSTRUCTION_SITES)
if (!(buildSites.length === 0)) {
if (creep.build(buildSites[0]) === ERR_NOT_IN_RANGE) {
creep.moveTo(buildSites[0])
}
}
else if (!(repairSites.length === 0)) {
if (creep.repair(repairSites[0]) === ERR_NOT_IN_RANGE)
creep.moveTo(repairSites[0])
}
else {
if (creep.upgradeController(creep.room.controller) === ERR_NOT_IN_RANGE) {
creep.moveTo(creep.room.controller)
}
}
}
}
}
module.exports = roleUpgrader