Trouble with telephoto glasses, specifically distanceTo target


#1
loop:
    enemy = self.findNearest(self.findEnemies())
    munchkin = self.findNearest(self.findByType("munchkin"))
    if enemy:
        if munchkin:
            distance = self.distanceTo(self.findByType("munchkin"))
            if distance < 10:
                if self.isReady("cleave"):
                    self.cleave(munchkin)
            elif self.isReady("bash") and distance <5:
                self.bash(munchkin)
            else:
                self.attack(munchkin)
        else enemy:
            self.attack(enemy)
    else:
        self.shield()

Trying to get a handle on some new glasses… not sure about whether or not I’ve properly figured out how to find and attack a specific type of enemy. I keep getting a distance to target unit error. I appreciate any comments which can help. Sorry in advance for possible noob mistake of posting in an incorrect category. I’m in Cavern Survival if that makes a difference.


#2

The distanceTo method requires 1 object to be fed into it. Find by type returns an array or list of objects. You are giving a lot of objects to a method which can only take one object causing the code to crash.

The following should fix your code:

distance = self.distanceTo(self.findNearest(self.findByType("munchkin"))):

or since you have already saved the nearest munchkin as a variable:

distance = self.distanceTo(munchkin)

#3

What Mr. Hinkle said, plus:

    if enemy:           # this is not really needed
        if munchkin:
            # ...
        else enemy:     # 'else' doesn't need any expressions
            # ...
    else:
        # ...

should be something like:

    if munchkin:    # munchkins are enemies, right?
        # ...
    elif enemy:     # this could still be a munchkin, but...
        # ...
    else:
        # ...

#4

OK, cool. Got it working - thanks Hinkle and ant. Both very helpful answers.