Explanation ==ERR_NOT_IN_RANGE
-
Hi! I am pretty new to JavaScript, but I love the concpet of Screeps so it is giving me a hard time. Could anyone please explain to me why the ==ERR_NOT_IN RANGE is important?
if(creep.harvest(sources[0]) == ERR_NOT_IN_RANGE) {
creep.moveTo(sources[0]);
}I thought that if I give a creep order to creep.moveTo(sources[0]); and then creep.harvest(sources[0]); it should do the work...
Thanks.
-
You could call moveTo() first and then harvest() second, but the problem with that is, once your creep reaches the source, it will still call moveTo() on every tick. This will be a higher cpu cost, 0.2 cpu for harvest() and at least 0.2 cpu for moveTo(), for 0.4 cpu total. By checking for ERR_NOT_IN_RANGE and only doing the moveTo() in that case, it reduces your cpu cost to about 0.2 cpu total on every tick for this block of code. This is because when a method like harvest returns an error, it does not incur it's full constant 0.2 cpu cost, because the database is never written to.
-
Oh I see, thank you!