Play Home Contribute Github Join Us! Discourse Staff Members Github Contribution Guides Team

Easiest way to select the enemy hero

What the most straightfoward way to select the enemy hero in multiplayer battles?
Given that all hero’s have different types having to check for all types seems a bit complicated.

1 Like

I think they’re all named “Hero Placeholder”, so you could use that, but I personally just use the types, like Captain, Knight, Sorcerer, and so on.

Depends which side you play.

The Human hero is always Hero Placeholder
The Ogre hero is always Hero Placeholder 1

1 Like

I’m having a problems using variables for Hero Placeholder. It works just fine for targeting but I cant figure out how to use .pos and such. enemy.pos with work, with enemy = findNearestEnemy but not Ehero.pos with Hero set as Ehero = “Hero Placeholder”. I’m assuming it has something to do with “Hero Placeholder” being inside of quotes?

That is correct, “Hero Placeholder” is just a string of characters not an object…

You’d have to check all enemies (loop over list of enemies) for a matching “.id” ( == “Hero …”) and keep that one in eHero then you could do eHero.pos

1 Like

I’m trying to get my friends to target the “Hero Placeholder” only, ignoring the other enemies.

Something that achieves this in one step:

friend.findNearest(enemies).id("Hero Placeholder")

I try this but no luck. Any ideas?

 for friend in friends:
        self.command(friend, "attack", friend.findNearest( == "Hero Placeholder"))

I would prefer to clean up the code and just have a variable in there and pull the friend.findNearest(enemi… method out and just declare it in a variable. I think since there’s a second set of ‘=’ I errors out the variable


hero = friend.findNearest( == "Hero Placeholder")

I’ve been successful with using that method with if statements but i’m unsure how to do it with id’s

if enemy.type == "shaman": 
        #do stuff

One thing you can do is use Python’s list comprehension feature to do this:

enemyHero = friend.findNearest([e for e in friend.findEnemies() if is "Hero Placeholder"])

There are also techniques like filter, where you provide a function to determine whether there is a match out of the array. Lo-dash lets you do it with a shorthand, and also get the first match, like this:

enemyHero = _.find(friend.findEnemies(), {"id": "Hero Placeholder"})

So far I’ve had zero luck with this working. I’ve tried this

# Defeat the enemy hero in two minutes.
    enemies = self.findEnemies()
    nearestEnemy = self.findNearest(enemies)
    item = self.findNearest(self.findItems())
    # Your hero can collect coins and summon troops.
    if item:
        if > self.costOf("archer"):
    friends = self.findFriends()
    enemyHero = friend.findNearest([e for e in friend.findEnemies() if is "Hero Placeholder"])
    for friend in friends:
        self.command(friend, "attack", enemyHero)

I’m getting errors on the friend.Nearest and friend.findEnemies() not be a valid function. I’ve tried using “self” instead of “friend” and that says the target it null… Help :frowning:

Enemy hero id can be "Hero Placeholder" or "Hero Placeholder 1".

Examples for finding enemy hero (best to just add these to the top of the code, outside the main loop, and forget):

# Python
enemyHero = [e for e in self.findEnemies() if in ["Hero Placeholder", "Hero Placeholder 1"]][0]
// JavaScript
var enemyHero = this.findEnemies().filter(function (e) {
    return === 'Hero Placeholder' || === 'Hero Placeholder 1';

In mirror matches (Zero Sum, Ace of Coders) it’s easier to just do

# Python
enemyHero = self.findByType(self.type)[0] or self.findByType('knight')[0]
// JavaScript
var enemyHero = this.findByType(this.type)[0] || this.findByType('knight')[0];

as both players have the same hero type (or Tharin if their game session is bugged out).

friend.findNearest(enemies).id("Hero Placeholder")

this takes 1 friendly unit, then returns the nearest enemy from the array of enemies. From this enemy you are trying“Hero Placeholder”) - this is not a function that individual characters have.

By the way, I was wondering – is Lodash being exposed to user code actually expected behavior?

Yeah. At some point we should document it in a Programmaticon, but it’s there even before we get around to doing that.