[SOLVED] Hero does not attack in reaping fire

My hero does not attack the fangriders. This is my code:

def chooseStrategy():
    enemies = hero.findEnemies()
    # If you can summon a griffin-rider, return "griffin-rider"
    if hero.gold >= hero.costOf("griffin-rider"):
        hero.summon("griffin-rider")
        return "griffin-rider"
        if enemy and enemy.type == "fangrider":
            
            return "fight-back"
        else:
            return "collect-coins"
    # If there is a fangrider on your side of the mines, return "fight-back"
    # Otherwise, return "collect-coins"

def commandAttack():
    # Command your griffin riders to attack ogres.
    friend = hero.findNearest(hero.findFriends())
    enemy = hero.findNearest(hero.findEnemies())
    friends = hero.findFriends()
    enemies = hero.findEnemies()
    for friend in friends:
        if friend.type == "griffin-rider":
            hero.command(friend, "attack", enemy)
            pass

    
def pickUpCoin():
    # Collect coins
    item = hero.findNearestItem()
    if item:
        hero.moveXY(item.pos.x,item.pos.y)
    pass
def heroAttack():
    # Your hero should attack fang riders that cross the minefield.
    posX = enemy.pos.x
    type = hero.findByType("fangrider")
    enemy = hero.findNearest(type)    
    if enemy and enemy.type == "fangrider" and posX < 37:
        hero.attack(enemy)
        
    pass
    
    
while True:
    commandAttack()
    strategy = chooseStrategy()
    # Call a function, depending on what the current strategy is.
    if strategy == "griffin-rider":
        commandAttack()
    if strategy == "fight-back":
        heroAttack()
    if strategy == "collect-coins":
        pickUpCoin()```

Why have you got the second if and the else inside the first if? They will only run if the first if is true won’t they? So you’ll never attack.
Also:

In the if statement you’re using the .pos.x of an undefined enemy, then you define the enemy that you want to attack. Remember that code runs line by line from top to bottom and when you use a variable it only stores the value it had when you last defined it. It doesn’t refresh its value by itself. You need to do that with =. Here you could just rearrange the order you define the variables.
Danny

This topic was automatically closed 12 hours after the last reply. New replies are no longer allowed.