<?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[Question attacking hostile creeps]]></title><description><![CDATA[<p>I tried method <code>attack</code> on hostile creeps, but it didn't work. The creep just move towards a hostile creep but do not attack. Can any body show me some code?</p>
]]></description><link>http://screeps.com/forum/topic/3235/question-attacking-hostile-creeps</link><generator>RSS for Node</generator><lastBuildDate>Sun, 07 Jun 2026 17:42:48 GMT</lastBuildDate><atom:link href="http://screeps.com/forum/topic/3235.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 14 Mar 2022 02:34:51 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Question attacking hostile creeps on Invalid Date]]></title><description><![CDATA[<p>I tried method <code>attack</code> on hostile creeps, but it didn't work. The creep just move towards a hostile creep but do not attack. Can any body show me some code?</p>
]]></description><link>http://screeps.com/forum/post/16604</link><guid isPermaLink="true">http://screeps.com/forum/post/16604</guid><dc:creator><![CDATA[UndefinedCpp]]></dc:creator><pubDate>Invalid Date</pubDate></item><item><title><![CDATA[Reply to Question attacking hostile creeps on Invalid Date]]></title><description><![CDATA[<p><a class="plugin-mentions-user plugin-mentions-a" href="http://screeps.com/forum/uid/5981">@undefinedcpp</a> The only time I could see it not working, is if the room your creep was in was in &quot;safe mode.&quot; Could you post a replay please?</p>
]]></description><link>http://screeps.com/forum/post/16605</link><guid isPermaLink="true">http://screeps.com/forum/post/16605</guid><dc:creator><![CDATA[SemperRabbit]]></dc:creator><pubDate>Invalid Date</pubDate></item><item><title><![CDATA[Reply to Question attacking hostile creeps on Invalid Date]]></title><description><![CDATA[<p><a class="plugin-mentions-user plugin-mentions-a" href="http://screeps.com/forum/uid/77">@SemperRabbit</a> Sorry, but the &quot;safe mode&quot; was not the problem. I tracked the return value and found that it was the <code>ERR_INVALID_TARGET</code> error, but I still had no idea why that happened. I ran the following code in simulation.</p>
<pre><code class="language-javascript">module.exports.loop = function () {
    for(var creepname in Game.creeps) {
        var creep = Game.creeps[creepname];
        var hostile = creep.room.find(FIND_HOSTILE_CREEPS);
        if(hostile) {
            creep.say(&quot;!&quot;);
            var result = creep.attack(hostile);
            if(result == ERR_NOT_IN_RANGE) {
                creep.moveTo(hostile);
            }
            console.log(result);
        }
        else {
            creep.say(&quot;:)&quot;);
        }
    }
}
</code></pre>
]]></description><link>http://screeps.com/forum/post/16606</link><guid isPermaLink="true">http://screeps.com/forum/post/16606</guid><dc:creator><![CDATA[UndefinedCpp]]></dc:creator><pubDate>Invalid Date</pubDate></item><item><title><![CDATA[Reply to Question attacking hostile creeps on Invalid Date]]></title><description><![CDATA[<p>a <code> .find()</code> will return an <code>array</code> see <a href="https://docs.screeps.com/api/#Room.find" rel="nofollow">https://docs.screeps.com/api/#Room.find</a></p>
<p>while <code>.attack()</code> requires an object (specificlly, a Creep, Structure, or PowerCreep) to be passed to it <a href="https://docs.screeps.com/api/#Creep.attack" rel="nofollow">https://docs.screeps.com/api/#Creep.attack</a></p>
<p>Need to select one-target from the array, I am surprised though that the creep was moving towards it, as <code>moveTo()</code> should also require a specific target.</p>
<p>Side note, a find call would return an empty array if nothing was found, so your first if would still clear even with no targets.  Need to check <code>.length</code> or other methods to determine targets exist.</p>
<p>Would recommend <a href="https://wiki.screepspl.us/index.php/A_brief_guide_to_reading_the_Screeps:World_API_Documentation" rel="nofollow">https://wiki.screepspl.us/index.php/A_brief_guide_to_reading_the_Screeps:World_API_Documentation</a> and also checking out the Discord as its a bit faster / more dynamic to get feedback and answers to questions.</p>
]]></description><link>http://screeps.com/forum/post/16607</link><guid isPermaLink="true">http://screeps.com/forum/post/16607</guid><dc:creator><![CDATA[Donatzor]]></dc:creator><pubDate>Invalid Date</pubDate></item><item><title><![CDATA[Reply to Question attacking hostile creeps on Invalid Date]]></title><description><![CDATA[<p><a class="plugin-mentions-user plugin-mentions-a" href="http://screeps.com/forum/uid/1043">@Donatzor</a> Thank you for that and the problem has been solved.</p>
]]></description><link>http://screeps.com/forum/post/16608</link><guid isPermaLink="true">http://screeps.com/forum/post/16608</guid><dc:creator><![CDATA[UndefinedCpp]]></dc:creator><pubDate>Invalid Date</pubDate></item></channel></rss>