Spawn.TransferEnergy to creep



  • I'm sorry if this was posted but I really tried to use search and all that and I could not find the answer.

     

    So I am trying to get creeps to pick up energy from the spawn and use it. My creeps seem to move to the spawn, but the transfer will not go through. Here is my code currently. It's the last part where it says else { if Game.spawns.Spawn1.TransferEnergy.

     

    var roleBuilder = {

    run: function(creep) {

    if(creep.memory.building && creep.carry.energy == 0) {
    creep.memory.building = false;
    }
    if(!creep.memory.building && creep.carry.energy == creep.carryCapacity) {
    creep.memory.building = true;
    }

    if(creep.memory.building) {
    var targets = creep.room.find(FIND_CONSTRUCTION_SITES);
    if(targets.length) {
    if(creep.build(targets[0]) == ERR_NOT_IN_RANGE) {
    creep.moveTo(targets[0]);
    }
    }
    }
    else {
    if(Game.spawns.Spawn1.transferEnergy(creep, RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
    creep.moveTo(Game.spawns.Spawn1);
    }
    }
    };

    module.exports = roleBuilder;

     

    Thank you for your help in advance!



  • In the StructureSpawn description (http://support.screeps.com/hc/en-us/articles/205990342-StructureSpawn) the declaration of this functino is not requiring the resource type, but instead the amount (which is optional)

                 transferEnergy(target, [amount])

    Interestingly enough the function still returns -9 in case the creep is too far away, which is why it moves.

    Anyway as far as i know, The constant RESOURCE_ENERGY resolves to the string "energy", so when the creep arrives at the spawn, it does not know how to transfer "energy" units of energy to the creep. You could add an else bracket to check for other error codes returning, but if you go for Game.spawns.Spawn1.transferEnergy(creep), everything should be fine.