How can I make a generalized attack function (Python Help)


#1

Greetings!

I am trying to create a general attack function, which can be used by both my hero and the soldiers that are summoned. Here is the my attackNearestOtherEnemy() function, which takes a parameter called unit; when I call this function, I either pass “hero” to it, or “soldier” to it. However, I get an error saying “TypeError: can’t read protected property: attack”. So, is there something wrong with my code?

def attackNearestOtherEnemy(unit):
    enemies = unit.findEnemies()
    nearestEnemy = None
    nearestEnemyDist = 9999
    for enemy in enemies:
        dist = unit.distanceTo(enemy)
        if dist < nearestEnemyDist and  enemy.type != "sand-yak":
            nearestEnemyDist = dist
            nearestEnemy = enemy
            pass
    if nearestEnemy:
        while nearestEnemy.health > 0:
            unit.attack(nearestEnemy)

#2

Hi,
this function’s working except for this line:

imagine a soldier was used in the function:

soldier.attack(nearestEnemy)

Hope this helps.
:lion: :lion: :lion: