Can you see anything wrong here in my code?
-
It's a code for role.builder module, I was trying to get him to repair ramparts when built, so they wouldn't be destroyed, but it doesn't work. Is there anything you can see here?
var roleBuilder = {
run: function(creep) { if(creep.memory.building && creep.store[RESOURCE_ENERGY] == 0) { creep.memory.building = false; creep.say('ENERGY'); } if(!creep.memory.building && creep.store.getFreeCapacity() != creep.store.getCapacity()) { creep.memory.building = true; creep.say('WORK'); } if(creep.memory.building == true) { if(creep.memory.repairing == false){ var targets = creep.room.find(FIND_CONSTRUCTION_SITES); if(targets.length) { if(creep.build(targets[0]) == ERR_NOT_IN_RANGE) { creep.moveTo(targets[0], {visualizePathStyle: {stroke: '#ffffff'}}); } } var important = creep.room.find(FIND_STRUCTURES, { filter: (s) => s.hits < 2 && s.structureType == STRUCTURE_RAMPART }); if(important != undefined){ creep.memory.repairing = true; } } else{ if(creep.repair(important) == ERR_NOT_IN_RANGE){ creep.moveTo(important, {visualizePathStyle: {stroke: '#ffffff'}}); } } } else { creep.memory.repairing = false; let container = creep.room.find(FIND_STRUCTURES, { filter: s => s.structureType == STRUCTURE_CONTAINER && s.store[RESOURCE_ENERGY] > 0 }); if (container.length > 0) { if (creep.withdraw(container[0], RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) { creep.moveTo(container[0]); } } } }
};
module.exports = roleBuilder;
-
You need to store important in memory / global. The var won't last next tick.
In your current setup, it first has to clear repairing = false, then check for important, if found, then set to true. As you've cleared the if/else that tick it won't go to the else. When your creep then has repairing set to true, and tries to use important (which I believe should be undefined), you should probably get -7 ERR_INVALID_TARGET, which you only have a handle for if its not in range.
Similar to your states, easiest fix is to make the if(important != undefined) not only set repairing to true, but set the important target to something like creep.memory.important or creep.memory.repairTarget.
Two things to note: .find returns an array, if no items are found the array is returned empty which I think is still 'defined' so you may want to change that to .length like you have for targets. Also, you can't store live-objects in memory so when setting your important/repair target for your creep past one tick I'd recommend using the ID of the object (rampart), and then you can use https://docs.screeps.com/api/#Game.getObjectById on subsequent ticks to get the live version.
Troubleshooting tip for the future, use console.log() to look at various returns, states of vars, or locations in your if/else trees so you can tell where your code is ending up, or similar to how you have creep.say for looking for energy can set up statements to see where its at.
Hope that helps, would recommend joining slack and heading over to the #help channel in the future, its a bit more dynamic.
-
@donatzor Thanks! I'll try it out
-
@donatzor And... It worked!! Thanks a lot!!!!!