Play Home Contribute GitHub Join Us Discourse Staff Members GitHub Contribution Guides Team

OASIS distance problems


why can the self.distanceTo(enemy) not work in this level !?


it works for me… post some code.


if self.distanceTo(self.findEnemies)<10:



Thats because self.findEnemies returns an array of enemies. You need to get a single enemy and pass it into distanceTo

    if self.distanceTo(self.findNearest(self.findEnemies())) < 10:

should work. but i dont know python so just a guess


I keep getting a really stupid error that says find distance to target unit
(using python)


not stupid likely there’s no enemy around at the time. try breaking it apart and using an if enemy

    enemy = self.findNearest(self.findEnemies())
    if enemy:
    	if self.distanceTo(enemy) < 10:


This is the lesson that was supposedly learned in Patrol Buster. In general, if you are struggling with something in a level, go back and look at earlier levels for keys to the code.


I think I had that issue in one of the desert levels. It just kept complaining about distance like crazy on specific line.


that is my problem to


Having the same issue ever since I unlocked the desert world.

    nearest_yaks = self.findNearest(self.findEnemies())
    for yak in nearest_yaks:
        if self.distanceTo(yak) < 10:  # Error: Find the distance to a target unit.
            x = self.pos.x + 10
            y = self.pos.y
            self.moveXY(x, y)

While trying to debug, I printed yak with self.say(yak) and it outputs the string “distance”. So either yak is a string in my case or it’s an object (like I expect it to be) and has a method like __str__ overridden to return “distance”, but that doesn’t make much sense.


It’s because you are passing findEnemies through findNearest findNearest returns only the nearest single enemy. Your code looks like it’s expecting the results from findEnemies which returns all the enemies.


You’re very right! Another pair of eyes definitely helps when you have tunnel vision, thanks.