Hunter and Prey Help with Archers. Python

# Ogres are trying to kill your reindeer!
# Keep your archers back while summoning soldiers to attack.

def pickUpCoin():
    # Collect coins.
    coin = hero.findNearest(hero.findItems())
    hero.move(coin.pos)
    pass

def summonTroops():
    # Summon soldiers if you have the gold.
    if hero.gold > hero.costOf("soldier"):
        hero.summon("soldier")
    pass
    
# This function has an argument named soldier.
# Arguments are like variables.
# The value of an argument is determined when the function is called.
def commandSoldier(soldier):
    # Soldiers should attack enemies.
    enemy = soldier.findNearest(hero.findEnemies())
    hero.command(soldier, "attack", enemy)
    pass

# Write a commandArcher function to tell your archers what to do!
# It should take one argument that will represent the archer passed to the function when it's called.
# Archers should only attack enemies who are closer than 25 meters, otherwise, stay still.
def commandArcher(friend):
    enemy = friend.findNearest(hero.findEnemies())
    if enemy and friend.distanceTo(enemy) < 25:        
        hero.command(friend, "defend", {'x': friend.pos.x, 'y': friend.pos.y})
        
while True:
    pickUpCoin()
    summonTroops()
    friends = hero.findFriends()
    
    for friend in friends:
        if friend.type == "soldier":
            # This friend will be assigned to the variable soldier in commandSoldier
            commandSoldier(friend)
        elif friend.type == "archer":
            # Be sure to command your archers.
            commandArcher(friend)
            pass

There appears to be something wrong with my commandArcher function. Every single time an enemy shows up on screen they immediately move to attack them instead of defending their current position. And I can’t seem to find what I am missing.

You only command the archers to defend their position when they are closer than 25 meters to an enemy. Until you command the archers, they will use their default AI which is to attack anything on sight.

Try removing the if statement.

I removed the if statement and the exact same thing happened. They immediately moved to attack and didn’t defend their position.

You mean, if you remove the if and keep the hero.command(friend, "defend", {'x': friend.pos.x, 'y': friend.pos.y}), does the problem still persist?

Yes but I did solve my problem. I added a command(archer, ‘move’, archer.pos) before the if statement and they didn’t move. Not sure why the move is needed since with defend they shouldn’t move for their position. but…