2015-05-19 23 views
3

我有腳本部分必須獲取源ID並將它們存儲到內存中,但仍然無法正常工作,請幫助我。Screeps內存添加如何?

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; 
     } 
    } 
+0

另外,source1 [i] .id - 必須成爲內存列表中的源代碼,但它不會將該部分作爲變量,那爲什麼?我能做什麼?! –

+0

SO大聲笑,解決了,那很容易,我只是stuid小白:D –

+0

請標記爲答案的合法答案! – Gewure

回答

5

對於那些仍在尋找答案。

您可以輕鬆地將新內存部件添加到任何對象。

大多數項目應該是房間特定的,所以在大多數情況下,您應該使用房間內存添加對象。在這個例子中可以在一個房間裏添加內存爲每個源:

//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. 
 
    */ 
 
}

0

有一個很好的回答類似的問題; 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; 
    } 
}); 

這避免了在所有的房間循環建立的快捷方式等。

0

我使用下面的代碼來保存當前房間中的所有源代碼和一個屬性,這將允許我稍後將礦工蠕變分配給源代碼。

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){ 

    }