# findEnemies function

**URL:** <https://discourse.codecombat.com/t/findenemies-function/11446>\
**Category:** Uncategorized\
**Created:** [May 16, 2017, 2:57pm UTC](https://discourse.codecombat.com/t/findenemies-function/11446 "2017-05-16T14:57:34Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Shmoogy](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/shmoogy/32/6770_2.png) [@Shmoogy](https://discourse.codecombat.com/u/Shmoogy)\
**Post date:** [May 16, 2017, 2:57pm UTC](https://discourse.codecombat.com/t/findenemies-function/11446/1 "2017-05-16T14:57:34Z")

</div>

Hi, I’m a bit new here, and new to programming in general. I wanted to know what information the findEnemies function returns, specifically if I can get the enemy’s type/class from it.

Thanks.

---

<div class="post-metadata">

**Author:** ![user1](https://avatars.discourse-cdn.com/v4/letter/u/5daacb/32.png) [@user1](https://discourse.codecombat.com/u/user1)\
**Post date:** [May 16, 2017, 3:40pm UTC](https://discourse.codecombat.com/t/findenemies-function/11446/2 "2017-05-16T15:40:38Z")

</div>

The function returns an array (a list) of all enemies the hero can see. Each entry of the list will be addressed by a number, (called index) it contains information about the enemy,as id/name, pos /position, type etc.  
Go through the sarven desert levels and look at the HINTS (blue button) thoroughly to learn more.  
Regards!

---

<div class="post-metadata">

**Author:** ![Shmoogy](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/shmoogy/32/6770_2.png) [@Shmoogy](https://discourse.codecombat.com/u/Shmoogy)\
**Post date:** [May 16, 2017, 3:50pm UTC](https://discourse.codecombat.com/t/findenemies-function/11446/3 "2017-05-16T15:50:30Z")

</div>

@user1, I’m still pretty far from the desert, but I already have the glasses that let me findEnemies. I’m still figuring out how the functions work, but if I wanted to code my hero into attacking by class in a loop, how would I do it with that? It’s start off with:

```python
hero.findEnemies

```

and then would probably need to set a variable, maybe:

```python
longRangeAttacker = enemy.type(Thrower)

```

Then, finally

```python
hero.attack(longRangeAttacker)

```

I strongly suspect that I’m setting up the middle code wrong, though. Help much appreciated.

---

<div class="post-metadata">

**Author:** ![user1](https://avatars.discourse-cdn.com/v4/letter/u/5daacb/32.png) [@user1](https://discourse.codecombat.com/u/user1)\
**Post date:** [May 16, 2017, 5:29pm UTC](https://discourse.codecombat.com/t/findenemies-function/11446/4 "2017-05-16T17:29:30Z")

</div>

Well, you would need to understand the concept of array if you really wanted to use that.  
But here some examples (not tested, hope they work)

Some examples:  
python:

```python
e=hero.findEnemies()
hero.attack(e[0]) #attacks first enemy (has index 0)

#Now do some stuff with all enemies in list:
i=0
while i<len(e): # do the following for all enemies from e[0] up to the last one
    hero.say(e[i].id) #greet your enemy
    hero.moveXY(e[i].pos.x, e[i].pos.y) #pay him a visit
    if e[i].type="thrower":
        hero.say("there is a thrower on the loose...")
    i=i+1 #increment your index, to get the next enemy in the next iteration of the while-loop
    

```

But as said before - I would really recommend to go through the main path in sarven desert to get a full tutorial on how to uses arrays and while loops…  
Regards!

---

<div class="post-metadata">

**Author:** ![Shmoogy](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.codecombat.com/shmoogy/32/6770_2.png) [@Shmoogy](https://discourse.codecombat.com/u/Shmoogy)\
**Post date:** [May 25, 2017, 11:44am UTC](https://discourse.codecombat.com/t/findenemies-function/11446/5 "2017-05-25T11:44:06Z")

</div>

I’ve sharpened my skills a bit, and think I have an easier way of doing what I wanted, not that I articulated that so clearly. What I wanted was a way to code in how to have my hero prioritize attacking a particular type of enemy. If you have glasses good enough, then this works:

```python
while True:
    thrower = hero.findByType("thrower", hero.findEnemies())
    enemy = hero.findNearestEnemy()
    if thrower:
        hero.attack(thrower)
    elif enemy:
        hero.attack(enemy)

```

---

<div class="post-metadata">

**Author:** ![FlowerChi](https://avatars.discourse-cdn.com/v4/letter/f/71c47a/32.png) [@FlowerChi](https://discourse.codecombat.com/u/FlowerChi)\
**Post date:** [May 31, 2017, 3:14am UTC](https://discourse.codecombat.com/t/findenemies-function/11446/6 "2017-05-31T03:14:22Z")

</div>

also the array if you do not know already starts at 0 not 1 like in some block coding programs.
