Play Home Contribute Github Join Us! Discourse Staff Members Github Contribution Guides Team

Grim determination help errors [SOLVED]

Errors on line 31 any help is appreciated
also had to replace find by type warlock for some reason otherwise i cant post
also im using boss star 3

// Your goal is to protect Reynaldo

// Find the paladin with the lowest health.
function lowestHealthPaladin() {
    var lowestHealth = 99999;
    var lowestFriend = null;
    var friends = hero.findFriends();
    for(var f=0; f < friends.length; f++) {
        var friend = friends[f];
        if(friend.type != "paladin") { continue; }
        if(friend.health < lowestHealth && friend.health < friend.maxHealth) {
            lowestHealth = friend.health;
            lowestFriend = friend;
        }
    }
    return lowestFriend;
}

function commandPaladin() {
    // Heal the paladin with the lowest health using lowestHealthPaladin()
    // You can use paladin.canCast("heal") and command(paladin, "cast", "heal", target)
    // Paladins can also shield: command(paladin, "shield")
    // And don't forget, they can attack, too!
    var paladins = hero.findByType("paladin");
    var i = 0;
    var paladin = paladins[i];
    if (paladins) {
        for (i = 0;i < paladins.length; i++) {
            var weakest = lowestHealthPaladin();
            if (weakest && paladin.canCast("heal")) {
                hero.command(paladin, "cast", "heal", weakest);
            }
        }
        for (i = 0;i < paladins.length; i++) {
            var munchkins = hero.findByType("munchkin");
            var skeletons = hero.findByType("skeleton");
            var warlocks = 
            if (warlocks.length > 0) {
                var warlock = paladin.findNearest(warlocks);
                hero.command(paladin, "attack", warlock);
            } else if (skeletons.length > 0) {
                var skeleton = paladin.findNearest(skeletons);
                hero.command(paladin, "attack", skeleton);
            }
        }
    }
}



function commandPeasant() {
    var peasants = hero.findByType("peasant");
    for (var r = 0;r < peasants.length;r++) {
        var peasant = peasants[r];
        var coin = peasant.findNearestItem();
        var ob = {"x":coin.pos.x,"y":coin.pos.y};
        hero.command(peasant, "move", ob);
    }
}

while(true) {
    commandPaladin();
    commandPeasant();
    // Summon griffin riders!
    if (hero.gold > hero.costOf("griffin-rider")) {
        hero.summon("griffin-rider");
    }
    var griffins = hero.findByType("griffin-rider");
    if (griffins) {
        for (var i = 0;i < griffins.length;i++) {
            var griffin = griffins[i];
            var enemy = griffin.findNearestEnemy(); 
            hero.command(griffin, "attack", enemy);
        
        }    
    }
    
}

You have:

    var paladins = hero.findByType("paladin");
    if (paladins) 

What will be the value of if (paladins) ( true or false) if there are no paladins?

i changed it to
if (paladins.length > 0)
but im still getting the same error

    var i = 0;
    var paladin = paladins[i];  // the paladin is paladin[0]
    if (paladins.length > 0) {  // I don't think you need this line even if it is correct 
        for (i = 0;i < paladins.length; i++) {
            var weakest = lowestHealthPaladin();
            if (weakest && paladin.canCast("heal")) {
                hero.command(paladin, "cast", "heal", weakest); // you are commanding only paladin[0]
            }
        }

Did’t run your code. It easier to debug if you take a screenshot of the error.

Are there any paladins who get resurrected? I get that error when I try to command paladins or soldiers who have been resurrected by the enemy. (either a warlock or an enemy hero wizard in the multiplayer levels). If this is the case you can put something like if paladin.team == “humans”… or if paladin in friends… That’s python, but I’m sure you can do it in Javascript.
If not, could you go to when you get the error around your hero, pause the playback and take a screenshot of that.
:lion: :lion: :lion:

Thanks that caused the error i changed it to when i look for paladins
paladins = (“paladin”, hero.findFriends());
but now i get this error

New code

// Your goal is to protect Reynaldo

// Find the paladin with the lowest health.
function lowestHealthPaladin() {
    var lowestHealth = 99999;
    var lowestFriend = null;
    var friends = hero.findFriends();
    for(var f=0; f < friends.length; f++) {
        var friend = friends[f];
        if(friend.type != "paladin") { continue; }
        if((friend.health < lowestHealth) && (friend.health < friend.maxHealth)) {
            lowestHealth = friend.health;
            lowestFriend = friend;
        }
    }
    return lowestFriend;
}

function commandPaladin() {
    // Heal the paladin with the lowest health using lowestHealthPaladin()
    // You can use paladin.canCast("heal") and command(paladin, "cast", "heal", target)
    // Paladins can also shield: command(paladin, "shield")
    // And don't forget, they can attack, too!
    var paladins = hero.findByType("paladin", hero.findFriends());
    var paladin;
    var weakest;
      
    if (paladins.length > 0) {
        for (var l = 0;l < paladins.length; l++) {
            paladin = paladins[l];
            weakest = lowestHealthPaladin();
            
            if (weakest) {
                if (weakest.health > 0) {
                    if (paladin.canCast("heal")) {
                       //hero.say(weakest.id);
                       hero.command(paladin, 'cast', 'heal', weakest);
                    }
                }
            }
        }
        for (var k = 0;k < paladins.length; k++) {
            paladin = paladins[k];
            paladins = ("paladin", hero.findFriends());
            var munchkins = hero.findByType("munchkin");
            var skeletons = hero.findByType("skeleton");
            var warlocks = hero.findByType("warlock");
            if (skeletons.length > 0) {
                var skeleton = paladin.findNearest(skeletons);
                if (skeleton) {
                    hero.command(paladin, "attack", skeleton);    
                }
            } else if (munchkins.length > 0) {
                var munchkin = paladin.findNearest(munchkins);
                if (munchkin) {
                    hero.command(paladin, "attack", munchkin);    
                }
            } else if (warlocks.length > 0) {
                var warlock = paladin.findNearest(warlocks);
                if (warlock) {
                    hero.command(paladin, "attack", warlock);    
                }
            } 
        }
    }
}



function commandPeasant() {
    var peasants = hero.findByType("peasant");
    for (var r = 0;r < peasants.length;r++) {
        var peasant = peasants[r];
        var coin = peasant.findNearestItem();
        var ob = {"x":coin.pos.x,"y":coin.pos.y};
        hero.command(peasant, "move", ob);
    }
}

while(true) {
    commandPaladin();
    commandPeasant();
    // Summon griffin riders!
    if (hero.gold > hero.costOf("griffin-rider")) {
        hero.summon("griffin-rider");
    }
    var griffins = hero.findByType("griffin-rider");
    if (griffins) {
        for (var i = 0;i < griffins.length;i++) {
            var griffin = griffins[i];
            var enemy = griffin.findNearestEnemy(); 
            hero.command(griffin, "attack", enemy);
        
        }    
    }
    
}

What you should do is paladins = hero.findByType(“paladin”, friends) all in one line.
:lion:

Thanks that fixed everything

Hey just one more question what strategy did you use for this level i cant seem to get passed it?
Right now im having paladins heal lowest health paladin then attack nearest
and having griffins attack nearest
and peasants collect nearest coin what should i do instead

// Your goal is to protect Reynaldo

// Find the paladin with the lowest health.
function lowestHealthPaladin() {
    var lowestHealth = 99999;
    var lowestFriend = null;
    var friends = hero.findFriends();
    for(var f=0; f < friends.length; f++) {
        var friend = friends[f];
        if(friend.type != "paladin") { continue; }
        if(friend.health < lowestHealth && friend.health < friend.maxHealth) {
            lowestHealth = friend.health;
            lowestFriend = friend;
        }
    }
    return lowestFriend;
}

function commandPaladin(paladin) {
    // Heal the paladin with the lowest health using lowestHealthPaladin()
    // You can use paladin.canCast("heal") and command(paladin, "cast", "heal", target)
    // Paladins can also shield: command(paladin, "shield")
    // And don't forget, they can attack, too!
    var healguy = lowestHealthPaladin();
    var enemy = paladin.findNearestEnemy();
    if (paladin.canCast("heal") && healguy) {
        hero.command(paladin, "cast", "heal", healguy);
    }
    if (paladin.health < (paladin.maxHealth * 0.25)) {
        hero.command(paladin, "move", {"x":66,"y":40});
    }
    if (enemy) {
        hero.command(paladin, "attack", enemy);
    }
}

function commandGriffin(griffin) {
    var g = griffin.findNearestEnemy();
    if (g) {
        hero.command(griffin, "attack", g);
    }
}

function commandPeasant(peasant) {
    var coin = peasant.findNearestItem();
    hero.command(peasant, "move", coin.pos);
}

function commandFriends() {
    // Command your friends.
    var friends = hero.findFriends();
    for(var i=0; i < friends.length; i++) {
        var friend = friends[i];
        if(friend.type == "peasant") {
            commandPeasant(friend);
        } else if(friend.type == "griffin-rider") {
            commandGriffin(friend);
        } else if(friend.type == "paladin") {
            commandPaladin(friend);
        }
    }
}
hero.summon("griffin-rider");
while(true) {
    commandFriends();
    // Summon griffin riders!
    if (hero.gold > hero.costOf("griffin-rider")) {
        hero.summon("griffin-rider");
    }
    
}

see [SOLVED] Grim Determination - paladin problem and especially
Your paladins are not healing, not moving, only attacking.
So you can restructure function commandPaladin(paladin).
Possible code:

    // your code
    if (enemy){
        if (paladin.canCast("heal") && healguy) {
            hero.command(paladin, "cast", "heal", healguy);
        }
        else if (paladin.health < (paladin.maxHealth * 0.25)) {
            hero.command(paladin, "move", {"x":66,"y":40});
        }
        else{   
            hero.command(paladin, "attack", enemy);
        }    
    }
1 Like

yup missed 2 elses fixed everything thanks