# Bug with findNearest(target) or i miss something

**URL:** <https://discourse.codecombat.com/t/bug-with-findnearest-target-or-i-miss-something/2257>\
**Category:** Uncategorized\
**Created:** [January 9, 2015, 2:55pm UTC](https://discourse.codecombat.com/t/bug-with-findnearest-target-or-i-miss-something/2257 "2015-01-09T14:55:04Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![davis\_merrit](https://avatars.discourse-cdn.com/v4/letter/d/7c8e57/32.png) [@davis\_merrit](https://discourse.codecombat.com/u/davis_merrit)\
**Post date:** [January 9, 2015, 2:55pm UTC](https://discourse.codecombat.com/t/bug-with-findnearest-target-or-i-miss-something/2257/1 "2015-01-09T14:55:04Z")

</div>

Hey there.  
I’ve bought enhanced lenses but can not use findNearest method  
Here is a gif to show you the problem - [link to my DropBox](https://dl.dropboxusercontent.com/u/107212046/Some%20pic/CC_FindNearest.gif)

Python code is below:

```
 enemies = self.findEnemies()
 self.say(enemies) # Works fine, return array with 10 enemy names
 i = 0
 while i < len(enemies):
     enemy = self.findNearest(enemies)
     self.say(enemy) # Does not work. Return "Object object" instead of enemy name
     i += 1

```

The question: is this a bug or I do something wrong?

---

<div class="post-metadata">

**Author:** ![sotonin](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/sotonin/32/24089_2.png) [@sotonin](https://discourse.codecombat.com/u/sotonin)\
**Post date:** [January 9, 2015, 3:21pm UTC](https://discourse.codecombat.com/t/bug-with-findnearest-target-or-i-miss-something/2257/2 "2015-01-09T15:21:18Z")

</div>

There’s no bug there. enemy is the enemy object, not the name of the enemy. If you want the name you should be able to access the id property. [enemy.id](http://enemy.id) or strangely enough i’ve found this works too.

```python
String(enemy)

```

The biggest question to me is why you are getting an array of enemy names when saying enemies. That is an array of the enemy objects, not just their names. Perhaps there’s some sort of toString method going on when you say that specific object hidden behind the scenes.

On another note, good luck with my level 🙂 it’s a doosy

---

<div class="post-metadata">

**Author:** ![davis\_merrit](https://avatars.discourse-cdn.com/v4/letter/d/7c8e57/32.png) [@davis\_merrit](https://discourse.codecombat.com/u/davis_merrit)\
**Post date:** [January 9, 2015, 4:11pm UTC](https://discourse.codecombat.com/t/bug-with-findnearest-target-or-i-miss-something/2257/3 "2015-01-09T16:11:08Z")

</div>

Ok, thanks for reply.  
But I still dont get, why my code does not work. I want to kill 10 nearest enemies, And when I try this:

```
 enemies = self.findEnemies()
 while i < len(enemies):
     enemy = self.findNearest(enemies)
     self.attack(enemy)
     i += 1
 self.say("done")

```

I get 2 enemies down and part with say “done”. But I want to kill all enemies in my array.  
And here is a [gif](https://dl.dropboxusercontent.com/u/107212046/Some%20pic/CC_FindNearest_2.gif)

---

<div class="post-metadata">

**Author:** ![sotonin](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/sotonin/32/24089_2.png) [@sotonin](https://discourse.codecombat.com/u/sotonin)\
**Post date:** [January 9, 2015, 4:15pm UTC](https://discourse.codecombat.com/t/bug-with-findnearest-target-or-i-miss-something/2257/4 "2015-01-09T16:15:28Z")

</div>

In your current code. enemies is static. since you declare it before your loop. It won’t update with the latest coords of the enemies, etc.

move it into your while loop and you might have better luck. Is this your whole code or just a snippet?

---

<div class="post-metadata">

**Author:** ![davis\_merrit](https://avatars.discourse-cdn.com/v4/letter/d/7c8e57/32.png) [@davis\_merrit](https://discourse.codecombat.com/u/davis_merrit)\
**Post date:** [January 9, 2015, 4:30pm UTC](https://discourse.codecombat.com/t/bug-with-findnearest-target-or-i-miss-something/2257/5 "2015-01-09T16:30:24Z")

</div>

That’s all my code at this moment. I’m just trying to understand some parts of programming. Its looks so simply, but I still have problems with it ☹  
And I think its not possible with my current amount of HP. I have 495 right now. But I’ll do my best 😃  
Thanks for answer again.

---

<div class="post-metadata">

**Author:** ![jklauser](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/jklauser/32/1867_2.png) [@jklauser](https://discourse.codecombat.com/u/jklauser)\
**Post date:** [January 9, 2015, 5:29pm UTC](https://discourse.codecombat.com/t/bug-with-findnearest-target-or-i-miss-something/2257/6 "2015-01-09T17:29:35Z")

</div>

I think I see the problem, let me try translating your code into “English”:

```python
while i < len(enemies):
     enemy = self.findNearest(enemies)
     self.attack(enemy)
     i += 1

```

While i is less than the number of enemies  
Find the nearest enemy in the collection of enemies  
_Attack that enemy once_  
Increment i by one.

I believe you would need to be getting one hit kills for this to work the way you intend.

---

<div class="post-metadata">

**Author:** ![davis\_merrit](https://avatars.discourse-cdn.com/v4/letter/d/7c8e57/32.png) [@davis\_merrit](https://discourse.codecombat.com/u/davis_merrit)\
**Post date:** [January 9, 2015, 5:47pm UTC](https://discourse.codecombat.com/t/bug-with-findnearest-target-or-i-miss-something/2257/7 "2015-01-09T17:47:02Z")

</div>

jklauser, you are right. I do not use `while enemy.health > 0`, cause I have 1 hit kill.

---

<div class="post-metadata">

**Author:** ![sotonin](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/sotonin/32/24089_2.png) [@sotonin](https://discourse.codecombat.com/u/sotonin)\
**Post date:** [January 9, 2015, 6:25pm UTC](https://discourse.codecombat.com/t/bug-with-findnearest-target-or-i-miss-something/2257/8 "2015-01-09T18:25:53Z")

</div>

I’m not sure you will be able to get away with that on this level. only for munchkins. shamans throwers etc will take more than one hit even with the great sword. but that aside. does this work a little better for you?

```python
enemies = self.findEnemies()
while i < len(enemies):
  enemy = self.findNearest(enemies)
  self.attack(enemy)
  i += 1
  enemies = self.findEnemies()
self.say("done")

```

---

<div class="post-metadata">

**Author:** ![davis\_merrit](https://avatars.discourse-cdn.com/v4/letter/d/7c8e57/32.png) [@davis\_merrit](https://discourse.codecombat.com/u/davis_merrit)\
**Post date:** [January 9, 2015, 7:27pm UTC](https://discourse.codecombat.com/t/bug-with-findnearest-target-or-i-miss-something/2257/10 "2015-01-09T19:27:57Z")

</div>

**sotonin** , your answer really helped me and that part of code works fine.  
I have not seen the boss yet, but I’ve done second oasis. So half is done I hope 😃

---

<div class="post-metadata">

**Author:** ![sotonin](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/sotonin/32/24089_2.png) [@sotonin](https://discourse.codecombat.com/u/sotonin)\
**Post date:** [January 9, 2015, 7:28pm UTC](https://discourse.codecombat.com/t/bug-with-findnearest-target-or-i-miss-something/2257/11 "2015-01-09T19:28:28Z")

</div>

yep. 3 trials then the main boss!
