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

Sarven brawl help

I am using Tharin with the Quartz sense stone, mahogany glasses, dynamic flags, crude spike, workers glove, simple wristwatch, progamantion III, leather boots and just purchased emperor’s gloves

Witch means no workers gloves

@ducky i cant attack when i select a yak, and get killed

Maybe you forgot to see if an enemy is there? What do you mean by “selecting” a yak?

I think you’re distance is too small.
3 is the usual attacking distance.
Maybe try something bigger, like 5.
:lion: :lion: :lion:

Do you have a sword equipped or any armor?

You may want to switch from using hero.findNearestEnemy() to hero.findEnemies() to get an array of all the enemies and you can run through them with a ‘for’ loop so you don’t get stuck on one enemy. As you run through the for loop you can check what type of enemy and determine which one you want to attack. Below is a modified version of your original code using the enemies array. Don’t forget the while True: at the beginning.

enemies = hero.findEnemies()
for enemy in enemies:
    if hero.distanceTo(enemy) < 3:
        hero.attack(enemy)
    elif enemy.type == "thrower" or enemy.type == "ogre":
        hero.attack(enemy)

@broosky125 I am using the rune sword and have both the rusted iron helmet and the rusted iron chest-plate

here is my updated code

while True:
    enemies = hero.findEnemies
    enemy = hero.findNearestEnemy()
    for enemy in enemies:
        if hero.distanceTo(enemy) < 5:
            hero.attack(enemy)
        elif enemy.type == "thrower" or enemy.type == "ogre":
            hero.attack(enemy)

it comes up with an error saying TypeError: Need an object

You need to check whether there’s an enemy, not just what it’s type is, or how far away it is. :smile:
:lion: :lion: :lion:

this still comes up with the same error

while True:
    enemies = hero.findEnemies
    enemy = hero.findNearestEnemy()
    if enemy:
        for enemy in enemies:
            if hero.distanceTo(enemy) < 5:
                hero.attack(enemy)
            elif enemy.type == "thrower" or enemy.type == "ogre":
                hero.attack(enemy)

well, the nearest enemy is a yak my code does not do anything else

I tried that but, I did not do anything.

I am going to be gone Monday and Tuesday so I will not be able to fill this out then.

Try if enemy.type!="yak":

You are using the same variable name for two different variables. If you want to use both lines of code, change the name of one of the enemy variables

enemy = hero.findNearestEnemy() 
for enemy in enemies:

and it is not yak, but sand-yak.

enemy.type != "sand-yak"
while True:
    enemies = hero.findEnemies
    boss = hero.findNearestEnemy()
    if boss:
        for boss in enemies:
            if hero.distanceTo(boss) < 5:
                hero.attack(boss)
            elif enemy.type == "thrower" or enemy.type == "ogre":
                hero.attack(boss)

is what i tried because it sounded like what you said but still didn’t work

what would I do with that?

Not quite, you are still using boss now for both variables. The variable used in the for loop is a temporary variable just for that loop. You can use whatever you want, just a long as it is not a variable used somewhere else in your code. The temporary variable does not have to be defined before the loop.

for temporaryVariable in enemies:

Also, you don’t need the two lines if you are focusing on the array (list) of enemies. And to explain what ducky was suggesting, you can check if the enemy is a sand-yak to skip over it, or you may accidentally attack it. See Below.

while True:
    enemies = hero.findEnemies
    for boss in enemies:
        if boss.type == "sand-yak":  # if it is a sand-yak, it will skip over and not attack
            continue
        elif hero.distanceTo(boss) < 5:
            hero.attack(boss)
        elif enemy.type == "thrower" or enemy.type == "ogre":
            hero.attack(boss)

If you want to use the other method too, assign a different variable name.

nearEnemy = hero.findNearestEnemy()

This is my final code that worked so this topic is solved.(I can’t solve it I don’t think

while True:
    enemy = hero.findNearestEnemy()
    if enemy:
        if enemy.type == "sand-yak": 
            hero.moveXY(25, 75)
            continue
        elif hero.distanceTo(enemy) < 5:
            hero.attack(enemy)
        elif enemy.type == "thrower" or enemy.type == "ogre":
            hero.attack(enemy)

Now replay the level and see how you do with the increased challenge. :thinking: