Failing as Commander in Sarven Siege


#1

In Sarven Siege I am trying to command my newly created soldier (at the tower where he’s created) to defend the tower instead of running off to distant enemies. However, I’m struggling with getting the defend command to work.

I get the error "Hero Placeholder can’t command type arrow-tower (only types: soldier, archer).

Since I’m not commanding the tower but a newly minted soldier, I’m stumped.

‘’’
// Defend your towers in this replayable challenge level!
// Step on an X if you have 20 gold to build a soldier.
loop{

var enemy = this.findNearest(this.findEnemies());
var closestGold = null;
var minGoldDistance = Infinity;
var coinIndex = 0;
var coins = this.findItems();
var friend = this.findNearest(this.findFriends());

while (this.gold < 21){
    var coin = this.findNearest(this.findItems());
    this.move(coin.pos);
}
if (this.gold > 21){
    if (this.pos.y < 36){
        this.moveXY(84, 22);
        this.command(friend, "defend", this.pos);
    }else{
        this.moveXY(84, 51);
    }
}

}
’’’


#2

Well, you need to add a type check then you define friend. Or you could use a while-loop to loop over all your friends, and command the ones whose type is “soldier”. For example:

var friendIndex = 0
while (friendIndex < this.findFriends().length) {
    // Initialize friend here.
    if (friend.type != "arrow-tower") {
        // Command your friend to do whatever.