Unexpected token {
-
Could someone help me? This was all I edited so it should be within here, but I keep getting unexpected token { errors.
if (4 < harvesters < 6){ if (guards < 4){ Game.spawns.Spawn1.createCreep(guard, null, {role: "guard"}) elseif(medics <3){ Game.spawns.Spawn1.createCreep(medic, null, {role: "medic"}) elseif (builders < 3){ Game.spawns.Spawn1.createCreep(builder, null, {role: "builder"}) elseif (harvesters < 16){ Game.spawns.Spawn1.createCreep(bodyExtractor, null, {role: "harvester"}) }; }; }; }; };
Also, if there is a better way to do what I am trying to do (As in, build these first, then these, then these after we have enough of each etc), then please tell me. But my purpose here is to solve the error.
-
Please don't compare with three element at the same time. Separate them like
a < something && something < b
Yeah, life woud be a lot easier but javascript sadly won't understatement you.
-
You should get a bit deeper into javascript:
1) Multiple comparisons are not possible
4 < harvesters < 6
should be4 < harvesters && harvesters < 6
2) there is noelseif
, it is writtenelse if
3) your nesting is wrong, instead ofif { else if { } }
it should be
if{ } else if { } else if { }
-
Thanks guys. I did get it to work by following your corrections Chris. I also figured out that putting ; after a bracket will return errors. Thanks for helping guys
-
I'd also restructure it, it is too WET right now, you want your code DRY
One might argue that Game.spawns.Spawn1.createCreep is only one line, but this is a function call, that you WILL change as your screeps empire grows, you'll heave more than one spawn, createCreep may not succeed and you'd want to fall back to another one. So I'd split this loop to two logical sections
1)Pick creep
2)Create creepCode becomes:
```javascript
//Select what to build
var creepBody = null;
var creepMemory = null;
if (guards < 4){
creepBody = guard;
creepMemory = {role:"guard"}
}else if (medics < 3){
creepBody = medic;
creepMemory = {role:"medic"}
}
...
//Build
Game.spawns.Spawn1.createCreep(creepBody, null, creepMemory); //easy to replace the createCreep call, fall back when it fails, etc.//Now, let's get more javascriptey:)
var creepSetupsByRole = {
"guard" : {
"min": 4,
"body": [ATTACK, ATTACK, MOVE],
"max": 10,
"curr": 0,
"roleName" : 'guard',
"essential" : true //Do not try to build others unless have enough of this role
},
"medic" : {
"min": 4,
"body": [HEAL, HEAL, MOVE],
"max": 10,
"curr": 0,
"roleName" : 'medic'
};
//Count the current number of creeps with given role here.
//Possibly sort roles by prioritiesfor (var roleName in creepSetupsByRole){
var currRoleSetup = creepSetupsByRole[roleName];
if (currRoleSetup.curr < currRoleSetup.min){
var buildResult = tryBuildCreep(currRoleSetup);
if (!(buildResult < 0) && !currRoleSetup.essential){ //
break;
}
}
}function tryBuildCreep(creepSetup){
return Game.spawns.Spawn1.createCreep(creepSetup.body, null, {role: creepSetup.roleName});
}