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

Grim determination help errors [SOLVED]


#1

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);
        
        }    
    }
    
}


#2

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?


#3

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


#4
    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.


#5


#7

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:


#8

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);
        
        }    
    }
    
}


#9

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


#10

Thanks that fixed everything