get resource from spawn



  • Hi, Im trying to get resources from my spawn with this script:

            var spawn = Game.getObjectById('1142bfa3ca07ddd');
            if(creep.harvest(spawn) == ERR_NOT_IN_RANGE) {
                creep.moveTo(spawn, {visualizePathStyle: {stroke: '#FFAA00'}});
            }
    

    my screep just stands still

    what did i miss ?



  • You can't harvest a spawn, you can only harvest a source (Energy or Mineral.) For a spawn, you use the withdraw method.

    As such, the harvest method will return an invalid object error, not an out of range error.

    Also, always check to see if the object returned from Game.getObjectById() is legit, it will be null if it's not. Next, instead of trusting that no other errors will occur, use a range test instead of the "try it and move on failure" method the tutorial seems to like.