3

我正在玩 Screeps ( http://screeps.com ),我正在尝试使用 lodash 模块从其余的小兵中过滤我的收割机。下面的代码应该可以工作,但是当我运行它时,我得到一个ReferenceError: _ is not defined at <main>:6:18. 知道出了什么问题吗?

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

2 回答 2

9

使用 lodash 模块时,有必要在模块的开头将它要求为如下所示的 var,然后它应该可以工作:

var _ = require('lodash');
于 2014-11-21T17:44:05.073 回答
0

更新

您也可以通过这种方式编写代码:

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

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

自 2014-12-01 以来,无需以roomhacky 方式查找。现在有一个全局函数Game.getRoom()

OLD FRAGMENT:唯一的问题是room有价值,但你可以从例如那里得到它。Game.spawns.Spawn1.room.

于 2014-11-22T00:12:25.220 回答