Screeps Memory adds how?
I have a part of a script that needs to get the id of the sources and store them in memory, but still doesn't work, please help me.
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;
}
}
source to share
For those who are still looking for an answer.
You can easily add new pieces of memory to any object.
Most of the items should be room-specific, so in most cases you should use room memory to add objects. In this example, you can add memory for each source in the room:
//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
}
}
}
After this code, all your sources have memory.
Let's try it with a harvester: ( creep
is a variable from a module)
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.
*/
}
source to share
There is a similar question with a good answer; fooobar.com/questions/825680 / ... .
I wanted it to be a property of the room, so I changed the code like this:
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;
}
});
This avoids looping all rooms to customize shortcuts, etc., as above.
source to share
I used the following code to save all sources in the current room and a property that will allow me to later assign a miner creep to the source.
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};
}
}
And here is the code to loop through memory and look at each source.
for(var x in spawns.memory.roomSources){
}
source to share