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

Harrowland - hero dont move

Hy guy, i’ve another problem, in this level hero dont attack enemy… why? there is my code.

enemies = hero.findEnemies()
index= 0
while index <len ( enemies):
    enemy = enemies[index]
    if enemy == "captain":
        if hero.canCast("chainlightning", enemy):
            hero.cast("chain-lightning", enemy)
        if enemy.health > 100:
            hero.attack(enemy)
        else:
            hero.attack(enemy)
    index = index+1

I’m a bit confused. Is this all of your code? And not all heroes have the type “Captain”, only Anya.
Could you tell me a bit more about your problem.
:lion: :lion: :lion:

Hy Deadpool198…
yes there is all my code… my hero is anya and in this level my opponent is an AI of anya…

When i start, my hero dont attack any enemies.

Put all of the code there inside a while True loop.

1 Like

The thing you need to keep in mind is that you will be fighting other heroes in this multiplayer level. While the original AI shows your hero, the actual fights won’t be the same hero oftentimes. For example, the list below shows multiple heroes that you will fight against. If you specify your code to just fight against Anya, you will lose a lot of battles.

Harrowland%20characters

The post below provides a pretty detailed line of code to ensure you find the opposite hero for the multiplayer regardless of what type.

Sorry, guy, but i’m confused…
i try this simple code… but my hero attack only 1 enemy and stop.

enemies= hero.findEnemies()
index = 0

while index < len(enemies):
    enemy = enemies[index]
    if (enemy):
        hero.attack(enemy)
        index += 1

Do you have all of this in a while True loop? Otherwise, you are only running through the code once. Also, I noticed that you have enemy in (), Python doesn’t need that. One other thing, in this case, I think it is better to have the index increment outside of your if statement (see below). Otherwise, if there isn’t an enemy, you will get stuck in an infinite loop.

while index < len(enemies):
    enemy = enemies[index]
    if enemy:
        hero.attack(enemy)
    index += 1
1 Like

Hy brooksy125, i change code and insert while true loop and move index increment, but hero only attack 1 enemy… sorry, but i dont understand…

while True:
    enemies= hero.findEnemies()
    index = 0
    
    while index < len(enemies):
        enemy = enemies[index]
        if enemy:
            hero.attack(enemy)
        index += 1

Hmm, not sure what to tell you, but the code works for me. I even copied straight from your post. What gear do you have? Is your hero dying?

Try something else for me. Add a hero.say like below.

        if enemy:
            hero.say(enemy.id) # add this line to see the names you are attacking
            hero.attack(enemy)

By doing this you can see if your hero says more than one name. Also, the enemies are identified and it will show the second part enemy as the one currently indexed.
enemy%20Gary enemy%20Korra enemy%20Tina

This is my gear:

my hero have all energy…

i try to rec my match.

Ok, I figured it out. I switched to the blue and played the simple enemy and they have bad code and it somehow stops your code.

Go back to the ladder (Scala), and click Play as Blue to pick a different opponent. I tried Easy against Giant Dad and it works now.

My solution to this error (‘loop’ breaks multiplayer code) is to do one command at the start (you can always do at least one), preferably an AOE (area of effect) spell like chain-lightning, or cleave. Then your soldiers have the advantage and win it for you. I use the silver bomb, but chain-lightning would work just as well.
Remember to put it at the top of your code so nothing else runs before it.
:lion: :lion: :lion:

And @Deadpool198 knows best on this level!! :sunglasses: This becomes critical when you are trying to rank up in the multiplayer ladder and playing against other people’s code. Some background info, a while back they used to use ‘loop’ instead of ‘while True’ and when they switched, all of the old codes that include ‘loop’ now create an error and can lock up your code as you saw.

What makes this highly problematic is that this is the Simple CPU that you start out playing against and don’t realize the problem is with the opposing code and not yours. As you see above, I brought this up on the post about the (‘loop’ breaking multiplayer code) hoping we can at least avoid this error in the training portion.
Another trick to look for is the red X under the enemy hero which let’s you know that their code has an error in it and may be disrupting yours.