Why does role not get loaded onto creep's memory



  • Can anyone tell me when why this code is able to spawn a creep but does not have its role saved in memory?

    var maintainCreeps = function(){
    var minHarvesters = 1;
    var minBuilders = 3;
    var minUpgraders = 2;
    var creepWorkerBody = [WORK,WORK,CARRY,CARRY,MOVE,MOVE];

    var harvesters = _.filter(Game.creeps, (creep) => creep.memory.role == 'harvester');
    console.log('Harvesters: ' + harvesters.length);

    if(harvesters.length < minHarvesters) {
    Game.spawns.Spawn1.createCreep(creepWorkerBody, 'Harvester'+(harvesters.length+1).toString(), {role: 'harvester'});
    }

    var upgraders = _.filter(Game.creeps, (creep) => creep.memory.role == 'upgrader');
    console.log('Upgraders: ' + upgraders.length);

    if(upgraders.length < minUpgraders) {
    Game.spawns.Spawn1.createCreep(creepWorkerBody, 'Upgrader'+(upgraders.length+1).toString(), {role: 'upgrader'});
    }

    var builders = _.filter(Game.creeps, (creep) => creep.memory.role == 'builder');
    console.log('Builders: ' + builders.length);

    if(builders.length < minBuilders) {
    Game.spawns.Spawn1.createCreep(creepWorkerBody, 'Builder'+(builders.length+1).toString(), {role: 'builder'});
    }
    }
    module.exports = maintainCreeps;

  • Culture

    How do you garbage collect your dead creep's memory? When you run createCreep it will create the object in memory that tick, but the creep won't exist yet- if you check Game.creeps it won't be there. So if you are running your creep memory cleanup after you ran the createCreep command you're probably erasing it's memory before it spawns.


  • Culture

    PS- Join is in IRC/Slack if you want to ask these things in real time.



  • You nailed it, still had the leftover dead creep memory clean from the tutorial and was wiping during my creep spawn.  Set to check if creep spawner was spawning before doing the memory wipe - all good thank you!  I will check out the Channel, much appreciated.


  • Culture

    No problem, glad I could help. Welcome to the game!



  • You might wanna just move the cleanup code up to be the very first thing happening each tick. This way it'll only be called the tick after createCreep. Solves your problem and you don't need to check spawns



  • I actually moved it into my spawn check, and It is only called immediately before I try spawning any new creeps.  This reduces it's checks and fixed the problem.  Good suggestion though @Amadox