Mixed Units Tactics level like oil and water


#1

I get the following error message running my code below:

You need a string to check the cost of; one of [“soldier”, “archer”]

Strangely it highlights code I’ve used just fine in previous levels. The rest of my code is based on previous levels as well, so I’m stumped.

Can anyone help?

// Practice using modulo to loop over an array

// Choose the mix and order of units you want to summon by populating this array:
var summonTypes = ["soldier", "archer", "soldier", "archer", "aoldier", "soldier", "soldier", "soldier", "archer", "archer", "archer", "soldier", "archer", "archer"];

this.summonTroops = function() {
    // Use % to wrap around the summonTypes array based on this.built.length
    var type = summonTypes[this.built.length % summonTypes.length];
// Error highlighted line below
    if (this.gold >= this.costOf(type)) {
        this.summon(type);
    }
};
this.commandTroops = function(){
    var troops = this.findFriends();
    var troopSupply = troops.length;
    var t, ally, enemy;
    for(t = 0; t < troopSupply; t++){
        ally = troops[t];
        enemy = ally.findNearestEnemy();
        if (ally.type != "palisade") {
            this.command(ally, "attack", enemy);
        }
    }
};
this.pickUpNearestItem = function(){
    var nearestItem = this.findNearest(this.findItems());
    if (nearestItem) {
        this.move(nearestItem.pos);
    }
};
loop {
    this.summonTroops();
    this.commandTroops();
    this.pickUpNearestItem();
}


#2

summonTypes[4] is "aoldier". The proper type, I believe, is "soldier".


#3

Ah, I missed that typo. Thanks, after correcting it the error message went away.

However, I still have strange behavior: the first 2 summoned (one soldier and one archer) just stand there without attacking, all other summons afterwards go on the offensive.

I’ve run it multiple times with different heroes with the same starting behavior. Any idea what it could be?


#4

Do they see an enemy at the time that they are summoned? Maybe you need an if enemy check before commanding them to attack.