Game Screeps - Using the lodash module

3.4k views Asked by At

I'm playing Screeps (http://screeps.com) and I'm trying to use the lodash module for filtering my harvesters from the rest of the creeps. The code below should work but when I run it I get a ReferenceError: _ is not defined at <main>:6:18. Any idea of what's wrong?

var harvesters = _.filter(Game.creeps, {memory: 'harvester'});
if(_.size(harvesters) < 3 && Memory.creep_queue.length===0) {
     Memory.creep_queue.push('harvester') 
}
2

There are 2 answers

1
Lucas Azevedo On BEST ANSWER

When using the lodash module, it's necessary to require it into a var like below in the beggining of the module and then it should work:

var _ = require('lodash');
0
michalv On

UPDATED

You can write your code also in this way:

var harvesters = room.find(Game.creeps, {
    filter: {memory: 'harvester'}
});

if(harvesters.length < 3 && Memory.creep_queue.length === 0) {
     Memory.creep_queue.push('harvester');
}

Since 2014-12-01 it is not necessary to find room in a hacky way. Now there's a global function Game.getRoom().

OLD FRAGMENT: The only problem is to have room value, but you can get it from eg. Game.spawns.Spawn1.room.