I'm having some trouble getting locations stored in a creep's memory to work. For example, this code:
var sources = creep.room.find(FIND_SOURCES);
if(creep.memory.sourceLocationIndex == null) {
creep.memory.sourceLocationIndex = Math.floor(Math.random() * sources.length);
}
return sources[creep.memory.sourceLocationIndex];
Works perfectly, but this code:
if(creep.memory.sourceLocation == null) {
var sources = creep.room.find(FIND_SOURCES);
var sourceLocationIndex = Math.floor(Math.random() * sources.length);
creep.memory.sourceLocation = sources[sourceLocationIndex];
}
return creep.memory.sourceLocation;
Seems to fail once the creep moves. Is there a reason why this would happen? What should I be doing differently?
This took me a long time to figure out, but once you know what is wrong, it makes sense why it is not working.
You cannot store a game object in memory.
My alternative, which I have seen other people use also, is to store the ID instead.
creep.memory.sourceId = sources[sourceLocationIndex].id;
and later
var source = Game.getObjectById(creep.memory.sourceId);