How to cast "chain-lightning"


#1

I just bought the emperor’s gloves, and have been trying to cast the lightning spell, but every time I try to cast it, the hero never casts the spell, instead he just attacks the enemies. here is my code:
loop:
enemy = hero.findNearestEnemy()
if enemy:
hero.attack(enemy)
elif enemy:
if hero.canCast(“chain-lightning”, enemy):
hero.cast(“chain-lightning”, enemy)

Am I doing anything wrong?


#2
if enemy:
    hero.attack(enemy)
elif enemy:
    # do something

How do you think when the second part inside elif enemy will be executed? If there is an enemy, then the first if will work and elif won’t be checked.


#3

so then should I set two different situations to do different thing? maybe a different distance or something?
This is what I have now, but now the hero doesn’t do anything at all. It is getting stuck at hero.findNearestEnemy for some reason:
loop:
enemy = hero.findNearestEnemy()
if enemy and distance > 10:
distance = hero.distanceTo(enemy)
if hero.canCast(“chain-lightning”, enemy):
hero.cast(“chain-lightning”, enemy)
elif distance < 10 and enemy:
hero.attack(enemy)


#4

I don’t know it depends on when you want to use lightning. But as it was if-elif - can’t work for any actions.

It will give you the error.


I tell him "don't attack the yak!", he attacks the yak
#5

can you show me an example of how you would use chain-lightning correctly? That would be very helpful. Also, thanks for the help!


#6

hero.cast("chain-lightning", enemy)

I don’t know for which cases you want to use that spell. It’s just a spell, or an ability. Like "cleave". Use when it ready and for cases when it’s required for your tactic.

Sorry, I really want to help, but your question looks like: “how would I use attack/move/cleave”.


#7

That’s fine, I think I have it now though. This is what I have come up with, and it seems to be working:
loop:
enemy = hero.findNearestEnemy()
if enemy:
distance = hero.distanceTo(enemy)
if distance < 10:
hero.attack(enemy)
elif distance > 10:
if hero.canCast(“chain-lightning”, enemy):
hero.cast(“chain-lightning”, enemy)
else:
hero.shield()
pass

Thanks!


#8

This code has a problem. If there is no enemy, then distance is not defined and you’ll get an error.

Also, please, format you code and wrap it with triple ` . Because without it I can’t see indents.


#9

This code is for Sarven Brawl so there is always an enemy, that’s why that isn’t a problem. Also, I don’t know how to format my code, sorry about that


#10

It’s not a good practice to be so sure. It’s ok for start levels, but I recommend you to write reliable code further. It’s possible that an enemy will be hidden or to far and as the result you’ll get wrong results or errors.


#11

Ok, that’s good advice. I just looked back, and if there is no enemy, I wrote
Else:
hero.shield()
maybe could be why I don’t get an error if there is no enemy?


#12

Nope, you don’t get error because if your hero saw an enemy on the previous iteration of the loop, then distance is defined, So as the result if there is no enemy then your code check prev iteration value and you can get wrong and unexcepted results.


#13

This has never “just worked” for me so far.
there seem to be some special conditions to it.

enemy = hero.findNearestEnemy()
if enemy:
    if hero.canCast("chain-lightning", enemy):
        hero.cast("chain-lightning", enemy)
    else:
        hero.attack(enemy)
pass

This code never causes hero to cast chain lightning, so I guess, he never can??? (Tried in Clash of Clones).

What are the conditions for if hero.canCast("chain-lightning", enemy): to evaluate to True?


#14

Maybe delete , enemy in hero.canCast()


#15

It works for me:

while True:
    enemy = hero.findNearestEnemy()
    if hero.canCast("chain-lightning",enemy):       
        hero.cast('chain-lightning', enemy)


#16

Why? It’s there in the syntax…


#17

Maybe cause you’re using a different hero?
But why would the conditions (who knows them?) be met with one hero and not with another?