Harvesting energy from an unclaimed room



  • I am trying to get a creep to harvest energy from an unclaimed room adjacent to mine then return to mine to deposit the energy in an extension in my room, but it tells me that else is undefined. Please help!

    Here is my code:

    var roleouterharvester = {

    run: function(creep) {

        if(creep.carry.energy < creep.carryCapacity) 
    

    { creep.moveTo(new RoomPosition(4, 13, 'W58S46'));

    { var sources = creep.room.find(FIND_SOURCES);

            if(creep.harvest(sources[0]) == ERR_NOT_IN_RANGE) {
    
                creep.moveTo(sources[0], {visualizePathStyle: {stroke: '#ffaa00'}});
    
            }
    
        }
    

    }

        Else { 
    

    {creep.moveTo(new RoomPosition(48, 15, ' W59S46'));

    { var targets = creep.pos.findClosestByRange (FIND_STRUCTURES, {

                    filter: (structure) => {
    
                        return (structure.structureType == 
    

    STRUCTURE_EXTENSION)

                        && structure.energy < structure.energyCapacity;
    
                    }
    
            }
    

    });

            if(targets.length > 0) {
    
                if(creep.transfer(targets[0], RESOURCE_ENERGY) == 
    

    ERR_NOT_IN_RANGE) {

                    creep.moveTo(targets[0], {visualizePathStyle: {stroke: '#ffffff'}});
    
                }
    
            }
    
        }
    
    }
    

    };

    module.exports = roleouterharvester;

    Please excuse the weird formatting, it pasted weirdly.



  • Ngl it's very hard to read with that formatting.
    To post code, surround all the code with 3 backticks (``` code ``') 🙂

    Here it is formatted better

    var roleouterharvester = {
    
    run: function(creep) {
    
        if(creep.carry.energy < creep.carryCapacity) 
    { creep.moveTo(new RoomPosition(4, 13, 'W58S46'));
    
    { var sources = creep.room.find(FIND_SOURCES);
    
            if(creep.harvest(sources[0]) == ERR_NOT_IN_RANGE) {
    
                creep.moveTo(sources[0], {visualizePathStyle: {stroke: '#ffaa00'}});
    
            }
    
        }
    }
    
        Else { 
    {creep.moveTo(new RoomPosition(48, 15, ' W59S46'));
    
    { var targets = creep.pos.findClosestByRange (FIND_STRUCTURES, {
    
                    filter: (structure) => {
    
                        return (structure.structureType == 
    STRUCTURE_EXTENSION)
    
                        && structure.energy < structure.energyCapacity;
    
                    }
    
            }
    });
    
            if(targets.length > 0) {
    
                if(creep.transfer(targets[0], RESOURCE_ENERGY) == 
    ERR_NOT_IN_RANGE) {
    
                    creep.moveTo(targets[0], {visualizePathStyle: {stroke: '#ffffff'}});
    
                }
    
            }
    
        }
    
    }
    };
    
    module.exports = roleouterharvester;
    

    Potentially a fixed version?

    var roleouterharvester = {
    
    run: function(creep) {
        if (creep.carry.energy < creep.carryCapacity) { 
            creep.moveTo(new RoomPosition(4, 13, 'W58S46'));
            if (creep.room.name == 'W58S46') {
                var sources = creep.room.find(FIND_SOURCES);
    
                if (creep.harvest(sources[0]) == ERR_NOT_IN_RANGE) {
                    creep.moveTo(sources[0], {visualizePathStyle: {stroke: '#ffaa00'}});
                }
            }
        } else { 
            creep.moveTo(new RoomPosition(48, 15, ' W59S46'));
            var targets = creep.pos.findClosestByRange (FIND_STRUCTURES, {
                    filter: (structure) => {
                        return (structure.structureType == 
    STRUCTURE_EXTENSION && structure.energy < structure.energyCapacity);
                    }
            });
    
            if(targets.length > 0) {
                if(creep.transfer(targets[0], RESOURCE_ENERGY) == 
    ERR_NOT_IN_RANGE) {
                    creep.moveTo(targets[0], {visualizePathStyle: {stroke: '#ffffff'}});
                }
            }
        }
    }
    
    };
    
    module.exports = roleouterharvester;
    


  • your "Else" is capitalized...the correct keyword is "else"



  • @Lisp thank you for telling me how to format my code in the forum. thanks for editing the code, ill try it and see if it works.