我有脚本部分必须获取源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;
}
}
Run Code Online (Sandbox Code Playgroud)
对于那些仍在寻找答案的人.
您可以轻松地将新内存部件添加到任何对象.
大多数项目应该是特定于房间的,因此在大多数情况下,您应该使用房间内存来添加对象.对于此示例,我们为房间中的每个源添加内存:
//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
}
}
}Run Code Online (Sandbox Code Playgroud)
在此代码之后,所有源都有内存.
让我们用收割机尝试:( 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.
*/
}Run Code Online (Sandbox Code Playgroud)