enemy = self.findNearestEnemy()
if enemy: # First if
distance = self.distanceTo(enemy)
if distance < 5: # Second if
else: # this else corresponds to the second if
Let’s say there is an enemy. The
first if is true, so distance is now 4.5 (because I say so).
second if is now true aswell (because there is an enemy). You cleave the enemy, and he’s dead now.
The loop starts again. Because of your awesome cleave, all enemies are dead now.
enemy is now
first if becomes
distance stays the same value as before, which is still 4.5
second if sees that
4.5 < 5 and enters. You now try to target an
undefined-enemy. This results in … I actually don’t know what this results in, most likely the result you saw. I thought this just throws an error…
But your code would still be problematic. Given you didn’t found an enemy, but somehow
distance is greater than 5. You then would enter the
else-path. What happens? You
self.attack(undefined). Still strange behaviour.
Now comes the twist… I have no idea why the code runs through. This is most likely related to some kind of error-catching-mechanism, that catches the error silently and simply continues your code. Do you have by any chance a red circle with a white cross below your feet?
EDIT: I just tested your code… completly error-free. But as soon as I add a
enemy = self.findNearestEnemy() or a
self.say(distance) just before the
second if, I get
That’s it, I ask @nick to explain.