Count the number of screeps in room with a certain role?



  • Hello,

    I am trying to find a away to count the number of screeps in each room with a certain role so I can't set minimum number of roles for each room.  

    thanks 

     



  • you need to use a filter. There are some examples in the docs



  • lodash's _.countBy is great here. _.countBy(room.find(FIND_MY_CREEPS), 'memory.role'). For quicker answers, join the slack channel. Lots of helpful folk there.



  • lil late, but its a good question, that I myself had..so for those looking, another option to countBy

    I tend to use many variations of .sum like:

    var x = _.sum(Game.creeps, (c) => c.memory.role == 'builder');

    or other combos like

    var x = _.sum(Game.creeps, (c) => c.memory.role == 'heavyworker' && c.memory.workingRoom == "E58N24" && c.memory.position == 1&& c.memory.upgradeMe==false && c.memory.recycleMe==false); 

    ...or whatever else you might have defined in the memory