【问题标题】:Game Screeps - Using the lodash moduleGame Screeps - 使用 lodash 模块
【发布时间】:2015-01-19 22:42:27
【问题描述】:

我正在玩 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') 
}

【问题讨论】:

    标签: screeps


    【解决方案1】:

    当使用 lodash 模块时,有必要在模块的开头将其引入如下 var 中,然后它应该可以工作:

    var _ = require('lodash');
    

    【讨论】:

    • 不再需要这样做。默认情况下包含 lodash。
    【解决方案2】:

    更新

    你也可以这样写代码:

    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 月 1 日起,无需以 hacky 方式查找 room。现在有一个全局函数Game.getRoom()

    OLD FRAGMENT:唯一的问题是拥有room 值,但您可以从例如获取它。 Game.spawns.Spawn1.room.

    【讨论】:

      猜你喜欢
      • 2015-01-26
      • 2015-01-20
      • 1970-01-01
      • 1970-01-01
      • 2018-03-18
      • 1970-01-01
      • 2017-08-24
      • 2018-09-10
      相关资源
      最近更新 更多