FindNearest, findInRange - How to use in Screeps?

I am trying to use findNearest as follows:

var sources = creep.room.findNearest(Game.SOURCES) creep.moveTo(sources[0]); creep.harvest(sources[0]); 

and this is what I get:

 TypeError: undefined is not a function at module.exports:5:28 at <main>:11:6 

How to use this method and findInRange so that they do not cause this error?

+7
javascript screeps
source share
2 answers

There are a few things here:

  • findNearest() not in the room object. Simple fix var sources = creep.pos.findNearest(Game.SOURCES)
  • findNearest() does not return an array of objects, it returns a single object (in particular, the nearest object) or null . The fix is ​​to change what you have creep.moveTo(sources); (you can do sources only to avoid confusion)
  • You did not specify the code, but I'm going to guess that you are doing something like creep.room.findInRange() , and again this is not in the room, the object is in pos, so it will look like creep.pos.findInRange() .
  • It is difficult to say that the only functions in the room are find() , lookAt() , findPath() and makeSnapshot() , while pos has a few more (listed in the location of the positions in the documents).

If you look in the documentation here for the room and here for placing and scrolling down, you can see what functions are in the object.

+10
source share

As I can see, the documentation is updated and now there is sample code.

http://screeps.com/docs/RoomPosition.php

0
source share

All Articles