Get number of creeps with "x" role



  • I'm new to Javascript especially though I have a little experience in other languages like C#.

    I'm trying to figure out how to call from the memory the number of creeps with a specific role so I can use that in my script. Sorry if it doesn't make sense, I'm trying to phrase it the best I can.



  • loop through Game.creeps, on each iteration you need to access the creep
    then

    if (creep.memory.role == "x") {//update your counting data structure}
    

  • Dev Team

    You can use lodash functions filter and size to achieve that:

    // Using Memory.creeps
    _(Memory.creeps).filter( { role: 'x' } ).size();
    
    // or using Game.creeps:
    _(Game.creeps).filter( { memory: { role: 'x' } } ).size();
    


  • Using native JS can be:

    var count = Object.keys(Game.creeps).reduce(function(p, c, i, a) {return p + (Game.creeps[c].memory.role == "aRole" ? 1 : 0)}, 0)
    

    Check out the reduce function.



  • Thanks for the help, guys. My current problem is this:

    var harvesters = _(Game.creeps).filter( { memory: { role: 'x' } } ).size();
    if (harvesters < 5){
    Game.spawns.Spawn1.createCreep(harv, null, {role: "harvester", set: "extractor"})
    };
    

    I'm trying to tell the game to stop producing harvesters after we have 5 out. I'll expand that later to depend on my energy situation, but this is currently for testing. As it is it completely ignores what I said and my spawn is producing a 6th harvester. How can I fix that?



  • The Game.creeps.myCreep object and the Memory.creeps.myCreep object are two different objects, NOT a reference to the same object. After a createCreep call, these two objects (from what I've seen) become exposed to our code add different times. This subtle timing difference has caught me a few times. Check out my response to this post for a more detailed look at the timings I've observed.



  • Thanks JMC, but I'm not really sure I understand what you observed in the other post relates here. I'm still pretty new to coding so it might just be my own problem, but did I mis-order my code that caused a problem? Please elaborate if you can.

    My main goal here is to compare the number of creeps in existence with a specified role (ex: 'harvester') to a constant or variable, in this case the constant 5. My idea for the code was that once that many creeps exist, then it would stop producing more, so I don't overflow with almost a hundred harvester creeps (I currently am doing that in another tab in the custom game section). How could I amend (or really, if need be rewrite) my code to heed the limit I place?



  • Sorry for the misleading post. Your right, it didn't relate to your issue (as far as I can tell). I copied your code into the sim room and just had to tweak a few things to get it to work. Here is the result:

    require("lodash")
    var harvesters = _(Game.creeps).filter( { memory: { role: 'harvester' } } ).size();
    if (harvesters < 5){
        Game.spawns.Spawn1.createCreep([MOVE], null, {role: "harvester", set: "extractor"})
    };
    

    As long as you have a Spawn1, it'll create 5 creeps then stop. The key change { role: 'x' } to { role: 'harvester' }



  • Oh dear. I actually, even after copy/pasting it into here, failed to realize i left role: 'x' in there. Meant to change that to harvesters. Thanks for your help, it finally works. I checked the memory, I had over 202 creeps spawn since I started it, which was hours ago and even running on 500% speed, it was still taking between 5-10 seconds per tick. There were easily over 100 creeps on screen. SO. MANY. HARVESTERS.