Question attacking hostile creeps
-
I tried method
attack
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?
-
@undefinedcpp The only time I could see it not working, is if the room your creep was in was in "safe mode." Could you post a replay please?
-
@SemperRabbit Sorry, but the "safe mode" was not the problem. I tracked the return value and found that it was the
ERR_INVALID_TARGET
error, but I still had no idea why that happened. I ran the following code in simulation.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("!"); var result = creep.attack(hostile); if(result == ERR_NOT_IN_RANGE) { creep.moveTo(hostile); } console.log(result); } else { creep.say(":)"); } } }
-
a
.find()
will return anarray
see https://docs.screeps.com/api/#Room.findwhile
.attack()
requires an object (specificlly, a Creep, Structure, or PowerCreep) to be passed to it https://docs.screeps.com/api/#Creep.attackNeed to select one-target from the array, I am surprised though that the creep was moving towards it, as
moveTo()
should also require a specific target.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
.length
or other methods to determine targets exist.Would recommend https://wiki.screepspl.us/index.php/A_brief_guide_to_reading_the_Screeps:World_API_Documentation and also checking out the Discord as its a bit faster / more dynamic to get feedback and answers to questions.
-
@Donatzor Thank you for that and the problem has been solved.