Withdrawing from spawns and containers
-
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
-
I am not sure, but after this code is executed
withdrawTargets.push(creep.room.find(FIND_STRUCTURES, { filter: (structure) => { return (structure.structureType === STRUCTURE_SPAWN) && (structure.energy > 0) } }))
You will have array of array in the
withdrawTargets
variable. So when you try to dowithdrawTargets[0]
you did have wrong type, array with StructureSpawns.
-
I changed that section of code as follows, and it is now working as intended. Thanks for your help.
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 = withdrawTargets.concat(creep.room.find(FIND_STRUCTURES, { filter: (structure) => { return (structure.structureType === STRUCTURE_SPAWN) && (structure.energy >= 300)}})) }
For the sake of anyone else who has this problem in the future, I'll explain what I did:
As explained by Flyasd, my previous code was creating a two dimensional array, so I modified it to use withdrawTargets.concat, which simply creates a new array containing the contents of the previous two arrays, rather than creating an array of arrays.
-
Instead of concat, you could habe just added..., so:. push(... Otherarray)