Toil and trouble help javascript

Please help me my soldiers are not prioritizing witch

this.chooseTarget = function(friend){
    if ( friend.type == "soldier" ){
        var enemy = friend.findNearest(this.findByType("witch"));
        return enemy;
    } else if ( friend.type == "archer" ) {
        enemy = friend.findNearest(friend.findEnemies());
        return enemy;

loop {
    var friends = this.findFriends();
    for(var i=0; i < friends.length; i++) {
        // Use your chooseTarget function to decide what to attack.
        var friend = friends[i];
        var target = this.chooseTarget(friend);
        this.say(friend + target);
        if (target){
        this.command(friend, "attack", target);}


Note that the this.say() is drastically slowing down your orders. The say command takes about one second to execute, so your current code waits one second between commanding each friend—the soldiers will be using their default AI (attack nearest enemy) until you command them and will likely be dead by the time you order them to attack the witches.

After removing the say function, your code worked for me.

Also, make sure that you have glasses with enough range for your hero to be able to see the witches, otherwise you will need a different way to find the witches.

1 Like

thanks ultCombo it worked :relieved:

1 Like