I don't understand why i get this error
-
Hello everyone,
This is the code for my miner...
mine: function(creep){ let myContainer = Game.getObjectById(creep.memory.myContainer); if(myContainer && creep.pos.isEqualTo(myContainer.pos)){ if(creep.memory.mySource == 0){ creep.memory.mySource = creep.pos.findClosestByPath(FIND_SOURCES); }else{ creep.harvest(creep.memory.mySource); } }else{ creep.moveTo(myContainer, {reusePath: REUSE_PATH}); } }
It doesn't work, and it throws me the following error:
throw new Error("It seems you're trying to use a serialized game object stored in Memory which is not allowed. Please use `Game.getObjectById` to retrieve a live object reference instead.");
For other purposes, i don't store only objects' id, but the whole objects and don't have that issue.
This is how i must do it to have it working. (don't think anyone is interested, but who knows)
mine: function(creep){ let myContainer = Game.getObjectById(creep.memory.myContainer); if(myContainer && creep.pos.isEqualTo(myContainer.pos)){ if(creep.memory.mySource == 0){ creep.memory.mySource = creep.pos.findClosestByPath(FIND_SOURCES).id; }else{ creep.harvest(Game.getObjectById(creep.memory.mySource)); } }else{ creep.moveTo(myContainer, {reusePath: REUSE_PATH}); } }
So, dear fellows: Why can't i do this?
-
You need to store the source by its id and each tick retrieve it by using
Game.getObjectById();
This is because when you store the the source object straight in Memory it becomes outdated. So like say you store it in Memory and it has 2000 energy. When you mine the source and it has less energy the object you stored in Memory will still have 2000 energy. Also, because Memory gets parsed and serialised each tick the next tick after you store the object the object is no longer an instance of the class "Source".
-
@starwar15432 Didn't think about that! I knew how to solve, because the error itself told me how to, but dindn't know why was happening.
Thanks a lot!!