Assigning Variables to Enemies

It seems that when I assign variables to enemies, the first enemy I see gets the assignment, leaving the other ones untouchable. If I assign one enemy to enemy1, and then the other to enemy2, the first enemy will be “enemy1” and “enemy2”. If I assign more varaibels the first enemy in the line of sight will be enemy1, enemy2, enemy3…and so on.
Any way around this?

1 Like

Could you pls post a screenshot of what is happening? Also what level are you on?
Thanks!

Try this. This makes it so you can assign a variable to the nth enemy.

enemies=hero.findEnemies()
enemy=enemies[0] #This finds the first enemy 
enemy1=enemies[1] #This finds the second enemy
enemy2=enemies[2] #This finds the third enemy. 

you can do this instead finding each enemy 1 by 1

var enemy = hero.findNearestEnemy();
while (enemy){
   hero.attack(enemy);
}

I can still only target one enemy to attack. My character never picks up that there are two.

Put the var enemy=hero.findNearestEnemy();, into a while (true){ } loop. It checks for an enemy every frame and attacks it.

i meant to put the while enemy inside of a while true loop, sorry if i confused you

Hey guys thanks for the help. This level has been solved. I ditched the loops and just told the character to do everything step by step and that worked.

Put [SOLVED] on the title so everyone can see.

I’m sure Enderlord meant:

while(true){
    var enemy = hero.findNearestEnemy();
    while(enemy && enemy.health > 0)
        hero.attack(enemy);
}

if the condition is only while(enemy) and the hero has a weak sword the result will be the message:
butitsdead
and the hero will freeze.