Please Help. Blackwoods Brawl (python)


#1

Tell me please where an error in the code:

 loop:
    enemy = self.findNearestEnemy()
    distance = self.distanceTo(enemy)
    if self.isReady("cleave"):
        if distance < 10:
            self.cleave(enemy)
    else: 
        self.attack(enemy)

And why this error: distanceTo target is null. Does the target exist?(Use if?)
If this code is correct:

loop:
    enemy = self.findNearestEnemy()
    distance = self.distanceTo(enemy)
    if distance < 10:
        # Attack if they get too close to the peasant.
        self.attack(enemy)
    else:
        self.moveXY(40, 37)

Thanks in advance.


#2

place three ` characters (on keyboard next to 1) on a line of it’s own both before and after your pasted code for formatting so people can read it here.


#3

yes “enemy = findNearest()” can be null, even when it makes absolutely no sense for it to be null…

if enemy:
    distance = …

or in your current case it would prolly make sense to combine the ifs (since you only use distance for the if).

if enemy:
    if self.isReady(“cleave”) and self.distanceTo(enemy) < 10:
        self.cleave(enemy)
    else:
        self.attack(enemy)


#4

try to change it like this (like @Vlevo did). the code should only have a distance to enemy when an enemy is there:

if enemy:
   distance = self.distanceTo(enemy) # because now its for sure that an enemy is defined, so there is a distance wich can be defined.

so if u check an enemy for things, always make sure it is defined that an enemy is there. best code always has “if enemy: run some code”

or u use the much shorter way with and:

if enemy and self.distanceTo(enemy) <= x:
   #run some code