How to exclude certain enemies[SOLVED]

Right now in sarven brawl, though this has been a very big problem for me for a long time. I cannot exclude certain enemies.

enemy = hero.findNearestEnemy(not “sand-yak”)

enemy = hero.findNearestEnemy(type != “sand-yak”)

enemy = hero.findNearestEnemy(enemy.type != “sand-yak”)

This is a problem because the hero will sit there if the enemy is a sand yak and do nothing as he is repeatedly stabbed by a brawler who was not the closest enemy to him when he ran the command.
Trying to re-run the command won’t work either, because archers are farther away[than the sand yak].

I have also tried many ways of using Hero.findEnemies() but cannot figure out how to exclude attacking sank-yaks then. Even with 3.5k health, the sand-yaks kill me within the two minute time I have.

You need to do

enemy = hero.findNearestEnemy()
if enemy.type !="sand-yak":
    #whatever
1 Like

Would this be considered level help?

Maybe. IDK reallly. (20 chars)

Well, I put it there.

if enemy and enemy.type != "sand-yak":

Is a little better)

2 Likes

If i do what you are suggesting, the hero just sits there and gets stabbed repeatedly when the enemy the code identified is a sand yak and a brawler approaches after that code is run

i am using brawler as an example, but yeah he just sits there.

and if I re-run hero.findNearestEnemy, it will kill the ogres except for if there are archers or shaman farther away from the than a sank-yak is. If they are able to attack me from behind a sand-yak, then my hero will never attack them as it will always identify the sank-yak as the “nearest” enemy.

This will always be a fundamental problem with hero.findNearestEnemy: if the nearest enemy isn’t what you want, that function can’t help you. Unless you build it yourself, there’s no easy way to request a type of not "sand-yak".

hero.findEnemies is the tool to use. It returns a list of every enemy you can see, leaving it to you do manually filter the list.

The most intuitive way to do so is to iterate through all enemies, check if they’re a yak, and if not consider them a target.

# Python
non_yaks = []
for enemy in hero.findEnemies():
  if enemy.type != "sand-yak":
    non_yaks.append(enemy)

enemy = hero.findNearest(non_yaks)

[Python] If you’re comfortable with such loops, I suggest researching how to use list comprehensions, a shorthand for the same logic. Using the filter function is another more complex way to filter a list.

[JS] Javascript provides the .filter method on arrays which are great for this application.

1 Like

thank you [some extra text because apparently I have to have 20 characters]

…nevermind. using this code:

non_yaks = []
for enemy in hero.findEnemies():
    if enemy.type != "sand-yak":
        non_yaks.append(enemy)

while True:
    enemy = hero.findNearest(non_yaks)
    if enemy:
        hero.attack(enemy)
    pass

He kills the first guy, then stops.

Actually, no matter what I do, he just stops

You are defining non_yaks once, at the beginning of the simulation, such that the list does not update to include new enemies as the game moves on. You’ll want to find potential enemies inside your loop.

o so I need to put it inside the loop, like

while True:
    
    non_yaks = []
    for enemy in hero.findEnemies():
        if enemy.type != "sand-yak":
            non_yaks.append(enemy)
    
    enemy = hero.findNearest(non_yaks)
    if enemy:
        hero.attack(enemy)
    pass

it works thank you (20 characters text)

3 Likes

This topic was automatically closed 12 hours after the last reply. New replies are no longer allowed.