<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Withdrawing from spawns and containers]]></title><description><![CDATA[<p>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:</p>
<ol>
<li>the code to withdraw from the spawn is running</li>
<li>withdrawTargets[0] is definitely an object</li>
<li>withdrawTargets[0] when logged to the console returns the spawn</li>
</ol>
<p>Essentially, my question is, why does the following work:</p>
<p>if (creep.withdraw(Game.spawns['Spawn1'], RESOURCE_ENERGY) === ERR_NOT_IN_RANGE) {
creep.moveTo(Game.spawns['Spawn1'])
}</p>
<p>But not:</p>
<p>if (creep.withdraw(withdrawTargets[0], RESOURCE_ENERGY) === ERR_NOT_IN_RANGE) {
creep.moveTo(withdrawTargets[0])
}</p>
<p>My full code for roleUpgrader is below in case something else is causing the problem:</p>
<p>var roleUpgrader = {
run: function (creep) {
var withdrawTargets = creep.room.find(FIND_STRUCTURES, {
filter: (structure) =&gt; { return (structure.structureType === STRUCTURE_CONTAINER) &amp;&amp; (structure.store[RESOURCE_ENERGY] &gt; 0)}})
if (Game.spawns['Spawn1'].memory.timeSinceLastSpawn &gt; 50) {
withdrawTargets.push(creep.room.find(FIND_STRUCTURES, {
filter: (structure) =&gt; { return (structure.structureType === STRUCTURE_SPAWN) &amp;&amp; (structure.energy &gt; 0)}}))
}
if(creep.carry.energy === 0 &amp;&amp; creep.memory.upgrading === 'true') {
creep.memory.upgrading = 'false'
}
if(creep.carry.energy === creep.carryCapacity &amp;&amp; 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) =&gt; { return (structure.hits &lt; 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</p>
]]></description><link>http://screeps.com/forum/topic/2360/withdrawing-from-spawns-and-containers</link><generator>RSS for Node</generator><lastBuildDate>Thu, 11 Jun 2026 16:41:01 GMT</lastBuildDate><atom:link href="http://screeps.com/forum/topic/2360.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 24 Sep 2018 15:33:26 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Withdrawing from spawns and containers on Invalid Date]]></title><description><![CDATA[<p>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:</p>
<ol>
<li>the code to withdraw from the spawn is running</li>
<li>withdrawTargets[0] is definitely an object</li>
<li>withdrawTargets[0] when logged to the console returns the spawn</li>
</ol>
<p>Essentially, my question is, why does the following work:</p>
<p>if (creep.withdraw(Game.spawns['Spawn1'], RESOURCE_ENERGY) === ERR_NOT_IN_RANGE) {
creep.moveTo(Game.spawns['Spawn1'])
}</p>
<p>But not:</p>
<p>if (creep.withdraw(withdrawTargets[0], RESOURCE_ENERGY) === ERR_NOT_IN_RANGE) {
creep.moveTo(withdrawTargets[0])
}</p>
<p>My full code for roleUpgrader is below in case something else is causing the problem:</p>
<p>var roleUpgrader = {
run: function (creep) {
var withdrawTargets = creep.room.find(FIND_STRUCTURES, {
filter: (structure) =&gt; { return (structure.structureType === STRUCTURE_CONTAINER) &amp;&amp; (structure.store[RESOURCE_ENERGY] &gt; 0)}})
if (Game.spawns['Spawn1'].memory.timeSinceLastSpawn &gt; 50) {
withdrawTargets.push(creep.room.find(FIND_STRUCTURES, {
filter: (structure) =&gt; { return (structure.structureType === STRUCTURE_SPAWN) &amp;&amp; (structure.energy &gt; 0)}}))
}
if(creep.carry.energy === 0 &amp;&amp; creep.memory.upgrading === 'true') {
creep.memory.upgrading = 'false'
}
if(creep.carry.energy === creep.carryCapacity &amp;&amp; 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) =&gt; { return (structure.hits &lt; 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</p>
]]></description><link>http://screeps.com/forum/post/11360</link><guid isPermaLink="true">http://screeps.com/forum/post/11360</guid><dc:creator><![CDATA[universal123]]></dc:creator><pubDate>Invalid Date</pubDate></item><item><title><![CDATA[Reply to Withdrawing from spawns and containers on Invalid Date]]></title><description><![CDATA[<p>I am not sure, but after this code is executed</p>
<pre><code>withdrawTargets.push(creep.room.find(FIND_STRUCTURES, {
                filter: (structure) =&gt; {
                    return (structure.structureType === STRUCTURE_SPAWN) &amp;&amp; (structure.energy &gt; 0)
                }
            }))
</code></pre>
<p>You will have array of array in the <code>withdrawTargets</code> variable. So when you try to do <code>withdrawTargets[0]</code> you did have wrong type, array with StructureSpawns.</p>
]]></description><link>http://screeps.com/forum/post/11361</link><guid isPermaLink="true">http://screeps.com/forum/post/11361</guid><dc:creator><![CDATA[Flyasd1]]></dc:creator><pubDate>Invalid Date</pubDate></item><item><title><![CDATA[Reply to Withdrawing from spawns and containers on Invalid Date]]></title><description><![CDATA[<p>I changed that section of code as follows, and it is now working as intended. Thanks for your help.</p>
<p>var withdrawTargets = creep.room.find(FIND_STRUCTURES, {
filter: (structure) =&gt; { return (structure.structureType === STRUCTURE_CONTAINER) &amp;&amp; (structure.store[RESOURCE_ENERGY] &gt; 0)}})
if (Game.spawns['Spawn1'].memory.timeSinceLastSpawn &gt; 50) {
withdrawTargets = withdrawTargets.concat(creep.room.find(FIND_STRUCTURES, {
filter: (structure) =&gt; { return (structure.structureType === STRUCTURE_SPAWN) &amp;&amp; (structure.energy &gt;= 300)}}))
}</p>
<p>For the sake of anyone else who has this problem in the future, I'll explain what I did:</p>
<p>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.</p>
]]></description><link>http://screeps.com/forum/post/11362</link><guid isPermaLink="true">http://screeps.com/forum/post/11362</guid><dc:creator><![CDATA[universal123]]></dc:creator><pubDate>Invalid Date</pubDate></item><item><title><![CDATA[Reply to Withdrawing from spawns and containers on Invalid Date]]></title><description><![CDATA[<p>Instead of concat, you could habe just added..., so:. push(... Otherarray)</p>
]]></description><link>http://screeps.com/forum/post/11365</link><guid isPermaLink="true">http://screeps.com/forum/post/11365</guid><dc:creator><![CDATA[Saruss]]></dc:creator><pubDate>Invalid Date</pubDate></item></channel></rss>