What is wrong with my script?



  • var harvester = require('harvester');
    
    for(var name in Game.creeps) {
        var creep = Game.creeps[name];
        harvester(creep);
    }
    if (creep.memory.role == 'harvester')
    {
        if(creep.carry.energy < creep.carryCapacity) {
            var sources = creep.room.find(FIND_SOURCES);
            creep.moveTo(sources[0]);
            creep.harvest(sources[0]);
        }
        else {
            creep.moveTo(Game.spawns.Spawn1);
            creep.transferEnergy(Game.spawns.Spawn1)
        }
    }
      var harvester = require('harvester');
    
    for(var name in Game.creeps) {
        var creep = Game.creeps[name];
    
        if(creep.memory.role == 'harvester') {
            harvester(creep);
        }
    
        if(creep.memory.role == 'builder') {
    
            if(creep.carry.energy == 0) {
                creep.moveTo(Game.spawns.Spawn1);
                Game.spawns.Spawn1.transferEnergy(creep);
            }
            else {
                var targets = creep.room.find(FIND_CONSTRUCTION_SITES);
                if(targets.length) {
                    creep.moveTo(targets[0]);
                    creep.build(targets[0]);
                }
            }
        }
    }
    if(creep.memory.role == 'guard') {
        var targets = creep.room.find(FIND_HOSTILE_CREEPS);
        if(targets.length) {
            creep.moveTo(targets[0]);
            creep.attack(targets[0]);
        }
    }
    


  • it runs smoothly in main with no others put when I spawn this
    ~~~
    Game.spawns.Spawn1.createCreep( [WORK, CARRY, MOVE], 'Worker_Alpha', { role: 'harvester' } );
    //then
    Game.spawns.Spawn1.createCreep( [WORK, CARRY, MOVE], 'Builder_Alpha', { role: 'builder' } );
    ~~~
    Worker_Alpha freezes
    also I get an error saying harvester is not a function on line 5 and line 23



  • You have var harvester = require('harvester'); in your code twice. Also at the top of your code you are telling every one of your creeps to harvest even if they don't have the harvester role. You should remove all of your code from the top to the second time you define harvester.



  • now with this code
    ~~~
    var harvester = require('harvester');

    if (creep.memory.role == 'harvester')
    {
    if(creep.carry.energy < creep.carryCapacity) {
    var sources = creep.room.find(FIND_SOURCES);
    creep.moveTo(sources[0]);
    creep.harvest(sources[0]);
    }
    else {
    creep.moveTo(Game.spawns.Spawn1);
    creep.transferEnergy(Game.spawns.Spawn1)
    }
    }

    for(var name in Game.creeps) {
    var creep = Game.creeps[name];

    if(creep.memory.role == 'harvester') {
        harvester(creep);
    }
    
    if(creep.memory.role == 'builder') {
    
        if(creep.carry.energy == 0) {
            creep.moveTo(Game.spawns.Spawn1);
            Game.spawns.Spawn1.transferEnergy(creep);
        }
        else {
            var targets = creep.room.find(FIND_CONSTRUCTION_SITES);
            if(targets.length) {
                creep.moveTo(targets[0]);
                creep.build(targets[0]);
            }
        }
    }
    

    }
    if(creep.memory.role == 'guard') {
    var targets = creep.room.find(FIND_HOSTILE_CREEPS);
    if(targets.length) {
    creep.moveTo(targets[0]);
    creep.attack(targets[0]);
    }
    }
    ~~~
    I get this error
    [9:12:49 PM]TypeError: Cannot read property 'memory' of undefined
    at main:3:10
    at n:5:28406
    at Object.c.runCode:5:34007