[SOLVED] Friends can't find enemies

My friends can’t find any enemies is this like bug or a error in my code.
The Code:

# Defeat the enemy hero in two minutes.
def commandSoldier():
    enemy = friend.findNearest(enemies)
    if hero.gold >= hero.costOf("soldier"):
        hero.summon("soldier")
    friends = hero.findFriends()
    for friend in friends:
        if friend.type == "soldier":
            hero.command(friend, "attack", enemy)

def commandPaladin():
    enemy = friend.findNearest(enemies)
    if hero.gold >= hero.costOf("paladin"):
        hero.summon("paladin")
    friends = hero.findFriends()
    for friend in friends:
        if friend.type == "paladin":
            if friend.canCast('heal') and hero.health / 2 * 1.5 < hero.maxHealth:
                hero.command(friend, "cast", "heal", hero)
            else:
                hero.command(friend, "defend", hero)

def commandPeasant():
    if hero.gold >= hero.costOf("peasant"):
        hero.summon("peasant")
    friends = hero.findFriends()
    for friend in friends:
        if friend.type == "peasant":
            hero.command(friend, "buildXY", 'decoy', peasant.pos.x + 1, peasant.pos.y)

def coin():
    items = hero.findItems()
    for item in items:
        hero.move(item.pos)

while True:
    coin()
    commandSoldier()
    commandPaladin()
    commandPeasant()
    enemy = hero.findNearestEnemy()
    if enemy:
        if hero.isReady("goldstorm"):
            hero.cast("goldstorm")
        elif hero.isReady("mana-blast"):
            hero.manaBlast()
        elif hero.canCast("fear", enemy):
            hero.cast("fear", enemy)
        elif hero.canCast("raise-dead"):
            corposes = hero.findCorpses()
            if corposes >= 5:
                hero.cast("raise-dead")
        else:
            if hero.distanceTo(enemy) <= 10:
                hero.cast("drain-life", enemy)
            elif hero.distanceTo(enemy) <= 40:
                hero.attack(enemy)
    
        
    

This is a screenshot

1 Like

Try:

def commandSoldier():
    enemy = friend.findNearest(enemy)
    if hero.gold >= hero.costOf("soldier"):
        hero.summon("soldier")
    friends = hero.findFriends()
    for friend in friends:
        if friend.type == "soldier" and enemy:
            hero.command(friend, "attack", enemy)
3 Likes

Didnt Work (20 chars)

1 Like

Oh, I forgot, try

def commandSoldier():
    enemy = friend.findNearest(enemy)
    if hero.gold >= hero.costOf("soldier"):
        hero.summon("soldier")
    friends = hero.findFriends()
if friends:
    for friend in friends:
        if friend and friend.type == "soldier" and enemy:
            hero.command(friend, "attack", enemy)
3 Likes

Ah, I know why you have an error:

def commandSoldier():
    if hero.gold >= hero.costOf("soldier"):
        hero.summon("soldier")
    friends = hero.findFriends()
if friends:
    for friend in friends:
        if friend and friend.type == "soldier":
    enemy = friend.findNearest(enemy)
if enemy:
            hero.command(friend, "attack", enemy)
3 Likes

Thanks for the advice but it didnt help but instead another problem popped up

1 Like

You can use “Tab”.(20 chars)

3 Likes

Yeah I also tried that

# Defeat the enemy hero in two minutes.
def commandSoldier():
    if hero.gold >= hero.costOf("soldier"):
        hero.summon("soldier")
    friends = hero.findFriends()
    if friends:
        for friend in friends:
            if friend and friend.type == "soldier":
                enemy = friend.findNearest(enemy)
                if enemy:
                    hero.command(friend, "attack", enemy)

This came up
Screenshot 2020-07-20 at 10.04.22 PM

1 Like

Did it work?(20 chars)

3 Likes

No unfortunately (20chars)

1 Like
enemy  =  friend.findNearestEnemy()
2 Likes

Maybe it can work.(20 chars)

2 Likes

Thats what i put at first didnt work

1 Like

Sorry, now I dont know what is happening. @AnSeDra, can you help @weary?

2 Likes
def commandSoldier():
    if hero.gold >= hero.costOf("soldier"):
        hero.summon("soldier")
    friends = hero.findFriends()
    if friends:
        for friend in friends:
            if friend and friend.type == "soldier":
                enemy = friend.findNearestEnemy()
                if enemy:
                    hero.command(friend, "attack", enemy)

@AnSeDra, what is wrong?

1 Like

Here try to instead check if len(friends) > 0.

Andrei

1 Like

Thanks,@AnSeDra!
@weary, did it work?

2 Likes

Thats not the code but I could try.
This it code I’m currently using

def commandSoldier():
    enemy = friend.findNearestEnemy()
    if hero.gold >= hero.costOf("soldier"):
        hero.summon("soldier")
    friends = hero.findFriends()
    for friend in friends:
        if friend.type == "soldier":
            hero.command(friend, "attack", enemy)

This is the problem
Screenshot 2020-07-20 at 10.34.41 PM

1 Like

Try something like this

2 Likes

Before this try to move the friend.findNearestEnemy() line of the code.

Andrei

2 Likes