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)
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)
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”.
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
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.
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?
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.