findEnemies function


#1

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.


#2

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!


#3

@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:

hero.findEnemies

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

longRangeAttacker = enemy.type(Thrower)

Then, finally

hero.attack(longRangeAttacker)

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


#4

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:

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!


#5

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:

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

#6

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