[Resolved] Help with withdraw from container



  • So I'm trying to get move energy out of a container. I think the code I use returns -7 aka Invalid Target. I'm still new to coding.

    Game.creeps[CreepName].withdraw(Game.rooms[RoomName].find(FIND_STRUCTURES, {filter: (s) => s.structureType == STRUCTURE_CONTAINER}), RESOURCE_ENERGY)

    That is basically what I am trying to run. It targets the container correctly,

    *Edit. That code is not what i was accually running but a failed attempt at debugging though the console instead of the script itself. <= Why I failed.....



  • You made a mistake in your code, please check out game api document.
    Game.rooms[RoomName].find return array but Game.creeps[CreepName].withdraw accept object.



  • Welp, looked at my code and I had left it backwards. copied it from my harvesters' code and didn't switch true:false


    module.exports = {
    run: function(creep) {
    if(creep.memory.working == true && creep.carry.energy == 0) {
    creep.memory.working = false;
    }
    else if (creep.memory.working == false && creep.carry.energy == creep.carryCapacity) {
    creep.memory.working = true;
    }
    if(creep.memory.working == true) {
    var Container = creep.pos.findClosestByPath(FIND_STRUCTURES, {
    filter: (s) => s.structureType == STRUCTURE_CONTAINER
    && s.store[RESOURCE_ENERGY] > 0
    })
    if(creep.withdraw(Container, RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
    creep.moveTo(Container)
    }
    }
    else {
    var EnergyStructures = creep.pos.findClosestByPath(FIND_STRUCTURES, {
    filter: (s) => (s.structureType == STRUCTURE_SPAWN
    || s.structureType == STRUCTURE_EXTENSION)
    && s.energy < s.energyCapacity
    })
    if(creep.transfer(EnergyStructures) == ERR_NOT_IN_RANGE) {
    creep.moveTo(EnergyStructures)
    }
    }
    }
    };

    silly me 😜