You can do something like this:
if(Number(Harvesters.name.slice(-1)) < 3)
Game.spawns.Spawn1.createCreep([Game.WORK, Game.CARRY, Game.MOVE],'Harvester'+ Harvesters.length, 'Harvester');
The thing to note here is that index is not a property and the deceptive id property is unique and therefore not what you're looking for. This code instead looks at the name of the current creep and gets the last character, then turns that into a number, and compares it to 3.
The way you're assigning to memory is fine though. You could do an object there if you really wanted, but that seems unnecessary with how you have it set up right now. It would look like this:
Game.spawns.Spawn1.createCreep([Game.WORK, Game.CARRY, Game.MOVE],'Harvester'+ Harvesters.length, {role: 'Harvester'});
And you'd have to be sure to change your memory
checks to memory.role
.
Rereading and I think what I did is not really what you were asking. If I understand correctly this should work better (not pretty though)
module.exports = function() {
var totalHarv = 0;
for(var q in Game.creeps)
if (Game.creeps[q].memory == 'Harvester')
totalHarv++;
for(var i in Game.creeps) {
if(Game.creeps[i].memory == 'Harvester') {
while(totalHarv < 3) {
Game.spawns.Spawn1.createCreep([Game.WORK, Game.CARRY, Game.MOVE], 'Harvester'+ totalHarv, 'Harvester');
totalHarv++;
}
}
}
}
This loops through the creeps and increments a counter if it finds a Harvester, then later when you're trying to spawn and stuff, it can check to see if there are enough of them and add more if necessary.