【问题标题】:Screeps Memory adding how?screeps内存怎么加?
【发布时间】:2015-07-31 04:56:48
【问题描述】:

我有脚本部分必须获取源 ID 并将它们存储到内存中,但仍然无法正常工作,请帮助我。

    for(var name in Game.spawns)
    {
        var source1 = Game.spawns[name].room.find(FIND_SOURCES)
        for(var i in source1)
        {
           Memory[source1[i].id] ={};
           Memory[source1[i].id].workers = 0;
        }
    }

【问题讨论】:

  • 另外,source1[i].id - 必须在内存列表中成为源 id,但它不会将该部分作为变量,那为什么呢?我能做什么?!
  • 太棒了,解决了,这很容易,我只是个新手:D
  • 请将正确答案标记为答案!

标签: memory screeps


【解决方案1】:

对于那些仍在寻找答案的人。

您可以轻松地向任何对象添加新的内存部分。

大多数项目应该是特定于房间的,因此在大多数情况下,您应该使用房间内存来添加对象。对于这个例子,让我们为房间中的每个源添加一个内存:

//Lets first add a shortcut prototype to the sources memory:
Source.prototype.memory = undefined;

for(var roomName in Game.rooms){//Loop through all rooms your creeps/structures are in
    var room = Game.rooms[roomName];
    if(!room.memory.sources){//If this room has no sources memory yet
        room.memory.sources = {}; //Add it
        var sources = room.find(FIND_SOURCES);//Find all sources in the current room
        for(var i in sources){
            var source = sources[i];
            source.memory = room.memory.sources[source.id] = {}; //Create a new empty memory object for this source
            //Now you can do anything you want to do with this source
            //for example you could add a worker counter:
            source.memory.workers = 0;
        }
    }else{ //The memory already exists so lets add a shortcut to the sources its memory
        var sources = room.find(FIND_SOURCES);//Find all sources in the current room
        for(var i in sources){
            var source = sources[i];
            source.memory = this.memory.sources[source.id]; //Set the shortcut
        }
    }
}

在这段代码之后,你所有的资源都有内存。

让我们用收割机试试吧:(creep 是模块中的一个变量)

var source = creep.pos.findClosest(FIND_SOURCES, {
    filter: function(source){
        return source.memory.workers < 2; //Access this sources memory and if this source has less then 2 workers return this source
    }
});
if(source){ //If a source was found
    creep.moveTo(source);
    creep.harvest(source);

    /* You should also increment the sources workers amount somehow, 
     * so the code above will know that another worker is working here. 
     * Be aware of the fact that it should only be increased once!
     * But I will leave that to the reader.
     */
}

【讨论】:

    【解决方案2】:

    有一个类似的问题有一个很好的答案; https://stackoverflow.com/a/30150587/5857473.

    我希望它成为房间的属性,所以我将代码更改为:

    Object.defineProperty(Source.prototype, 'memory', {
        get: function() {
            if(_.isUndefined(this.room.memory.sources)) {
                this.room.memory.sources = {};
            }
            if(!_.isObject(this.room.memory.sources)) {
                return undefined;
            }
            return this.room.memory.sources[this.id] = this.room.memory.sources[this.id] || {};
        },
        set: function(value) {
            if(_.isUndefined(this.room.memory.sources)) {
                Memory.sources = {};
            }
            if(!_.isObject(this.room.memory.sources)) {
                throw new Error('Could not set source memory');
            }
            this.room.memory.sources[this.id] = value;
        }
    });
    

    这避免了循环遍历所有房间来设置快捷方式等。

    【讨论】:

      【解决方案3】:

      我使用以下代码保存了当前房间中的所有源和一个属性,以便我稍后将矿工cree分配给源。

          if(!spawns.memory.roomSources){
              spawns.memory.roomSources=[];
              var energySources = spawns.room.find(FIND_SOURCES);
              for(var i in energySources){
                  spawns.memory.roomSources[i] = {sourceId: energySources[i].id, currentMinerId: null};
      
              }
          }
      

      这是遍历内存并查看每个源代码的代码。

          for(var x in spawns.memory.roomSources){
      
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-01-24
        • 1970-01-01
        • 2015-01-19
        • 2011-08-09
        • 2018-07-27
        • 1970-01-01
        • 2011-05-31
        相关资源
        最近更新 更多