Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

findNearest, findInRange - How to use in Screeps?

I try to use findNearest like that:

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 don't cause this error?

like image 212
Krokiet Avatar asked Nov 21 '14 08:11

Krokiet


People also ask

How do you spawn a creep in Screeps?

An AI can request the spawn to spawn new creeps. When enough energy is available in its storage device, the required energy will be subtracted from it and use it to spawn new creeps. The spawn is important because it's one of the first elements available in the game that can be used.


1 Answers

There are several things to note here:

  1. findNearest() is not in the room object. Simple fix var sources = creep.pos.findNearest(Game.SOURCES)
  2. findNearest() does not return an array of objects, it returns one single object (specifically the closest object) or null. The fix here is to change what you have to creep.moveTo(sources); (you might want to make sources singular to avoid confusion)
  3. You didn't provide code but I'm going to guess you're doing something like creep.room.findInRange() and again it's not in the room object it's in pos so it would look like this instead, creep.pos.findInRange().
  4. Confusingly, the only functions in room are find(), lookAt(), findPath(), and makeSnapshot() whereas pos has quite a few more (listed in roomposition in the docs)

If you look in the documentation here for room and here for roomposition and scroll to the bottom you can see which functions are in which object.

like image 153
dlkulp Avatar answered Oct 09 '22 13:10

dlkulp