Need help with Repair Creep Code



  • I'm trying to make a basic creep that will go and repair very damaged buildings around my base, but the creep seems to do nothing even though I'm getting no errors.

    My code for the repair creep:

    var roleRepairer = {
     
        /** @param {Creep} creep **/
        run: function(creep) {

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

            if(creep.memory.repairing) {
                var targets = creep.room.find(FIND_STRUCTURES, {
                 filter: object => object.hits < (object.hitsMax/4)
                });

                targets.sort((a,b) => a.hits - b.hits);

                if(targets.length > 0) {
                    if(creep.repair(targets[0]) == ERR_NOT_IN_RANGE) {
                creep.moveTo(targets[0]);    
        }
    }
            } else {
                var sources = creep.room.find(FIND_SOURCES);
                if(creep.harvest(sources[0]) == ERR_NOT_IN_RANGE) {
                    creep.moveTo(sources[0]);
                }
            }
        }
    };
    module.exports = roleRepairer;

     

    And in the main module i call

    var roleRepairer = require('role.repairer');

    and then run the role with

    if(creep.memory.role == 'repairer') {
                roleRepairer.run(creep);
            }

     



  • if(creep.memory.reparing && creep.carry.energy == 0) {        <- typo here on repaIring

     

    i suggest use console.log or creep.say to debug the execution path and find issues like this