Backwoods Brawl, array to find closest finds farthest

Here is the relevant code. After this it goes into if nearest: and attacking while health > 0. As far as I can tell, my less than/more thans are pointing the right way, but it keeps going for far off throwers instead of the swarm of munchkins around it. Where am I missing something?

    enemies = self.findEnemies()
    enemyIndex = 0 #edited to add to post but was already in code
    nearest = None
    nearDistance = 9999

    while enemyIndex < len(enemies):
        enemy = enemies[enemyIndex]
        if enemy:
            distance = self.distanceTo(enemy)
            if distance < nearDistance:
                nearest = enemy
                nearDistance = distance
        enemyIndex = enemyIndex +1

Did you ever set enemyIndex to 0?

Yes, sorry, I’m not sure how that disappeared. Editing that back in now.

Did it work? If so, thank @zuf

Sorry, to clarify: enemyIndex = 0 was already in my code, it got lost while I was formatting for the forum. enemyIndex = 0 is not the answer to the problem.

The code looks like it should work, and I tested it myself.

However, attack is uninterruptable. When you are ‘attacking’ you will move to the target and attack them until it is dead, then continue on. In my testing, I pick the closest enemy on the first frame (a thrower), then all the Munchkins swarm around me while I push towards the thrower… Then, after killing the thrower, I start attacking those nearest to me, (the munchkins.)

When you run the above code, is it always attacking the farthest each time or just once at the start? Because it should be working correctly.

WELP, I figured it out, while I was tracking exactly what happened and going over and commenting out swaths of code trying to figure out if anything deeper was the problem. I had “if nearest” and then “while > 0” and “self.attack(enemy)”. So I was attacking whatever was position 1 in the new array it found after having found the nearest ogre in this one (I believe).

Thanks for the help, y’all, I’m going to go lay my head on my desk and be amazed at myself for taking so long to notice that.

[details=what I WAS writing]On closer examination, it is not necessarily picking the farthest, but it is definitely not ever attacking the closest. When I hit run (not submit), here’s what happens:


  • Pick a thrower in the left clump, go attack it.
    -Thrower is dead, surrounded by left clump, right clump is approaching
  • Pick munchkin in right clump, go attack it (is not farthest, but is not closest)
  • Munchkin is dead, I am in a munchkin clump with wings of throwers, left slightly closer than right.
    -Pick thrower in right clump, go attack it
    -thrower is dead, I am surrounded by munchkins in middle of field, new throwers have appeared at 2 and 4 oclock
    -Pick 4oclock thrower, go attack it
    -thrower is dead, surrounded by munchkins, throwers about an inch away, ogre has appeared at 9 oclock
    -pick 9oclock ogre, go attack it
    -ogre is dead, surrounded by munchkins, trail of throwers and a dragonrider, thrower has appeared at 7 oclock
    -pick 7 oclock thrower, go attack it
    -thrower is dead, 2 new munchkins have appeared, one at 2 oclock and one an inch away,
    -pick the inch-away munchkin
    -dead before I reach it. (19 seconds) [/details]