How to identify a specific enemy to target based on type?

i’m trying to figure out how to get my boss (Tharin) to attack the enemy shaman if they spawn, but i can’t figure out how to get Tharin to “identify” the boss. I have no trouble getting him to “see” the enemy boss.

I want to get Tharin to attack the type of enemy i want him to , based on what enemies he finds in the “enemies” list, but i’m lost as to how i define that type.

this is what i’m trying to do, in a psudo-JavaScript i guess.

for(var i = 0; i< enemies.length;i++){
if(enemies[i].type == ‘shaman’){
this.attack([the nearest enemytype ‘shaman’]);

but i can’t find a way to define the enemy type that has been found in order to use it later.

It’s quite simple, and you’re overlooking it most likely.

for(var i = 0; i < enemies.length; i++) {
    if(enemies[i].type === 'shaman') {
        this.attack(enemies[i]);
    }
}

Since there is guaranteed to only be one shaman, since you can only have one hero, this will work. You’ve already determined the ith enemy in that array is the shaman, so just attack that exact enemy.

If you want to save it for later, you can do:

var enemyHero;
for(var i = 0; i < enemies.length; i++) {
    if(enemies[i].type === 'shaman') {
        enemyHero = enemies[i];
    }
}
if(enemyHero) { // done to ensure it's not null and that we did find the hero
    this.attack(enemyHero);
}

And of course you could use this.enemyHero instead if you wanted to save it over multiple frames.

  • Note that I used === to compare instead of ==. == lets you convert types, while === ensures the types are the same. As a general rule of thumb, always use === unless you explicitly need ==. This is specifically a JavaScript thing.

  • Also note that enemies[0] is always the base, so to make your code ever so slightly faster, you can start the loops from 1. 99% of the time, that should find the hero on the first loop.

1 Like

The code snippets in the if’/else description use enemy.type without specifying anything else. Is it necessary to have a particular item that allows you to reference an ‘enemies’ list or an enemy type?

I believe you will need at least Mahogany Glasses to use the self.GetEnemies method. The type property shouldn’t require any specific equipment.

1 Like

I thought so too but I have not been able to get enemy.type to work in the Siege of Stonehold, not that it’s required to beat that level, but it would have made things even more interesting.

I’ve noticed that if you try and used some advanced commands and tools on earlier levels, they sometimes don’t work. I ran into a similar problem using enemy.type when I went back and tried different approaches on the Kith and Forest worlds.

1 Like

Hmm, enemy.type should always work. Are you sure enemy is defined when you try to access type, and you are checking the correct type?

I decided to perform another test and sure enough it does work. Unfortunately I don’t have my original code to see what I was doing wrong last time.

loop {
    var enemy = this.findNearestEnemy();
    if (enemy) {
        var creatureType = enemy.type;
        var strAttackAnnounce = "I'm attacking a ";
        this.attack(enemy);
        this.say(strAttackAnnounce+creatureType);
    }
}