2014-11-21 97 views
2

我玩的屏幕(http://screeps.com),我正在尝试使用lodash模块来过滤我的收割机。下面的代码应该工作,但是当我运行它时,我得到一个ReferenceError: _ is not defined at <main>:6:18。有什么不对?游戏屏幕 - 使用lodash模块

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

回答

7

当使用lodash模块,有必要要求它到模块的beggining像下面一个变种,然后它应该工作:

var _ = require('lodash'); 
+0

Lodash已经在[simulation](https://screeps.com/a/#!/sim)中导入。 – Basilevs 2015-09-01 16:46:38

+0

不再需要这样做。 lodash默认包含在内。 – jfren484 2017-04-24 19:53:32

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以来,没有必要以黑客的方式找到room。现在有一个全球功能Game.getRoom()

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