Hi!
I know this is covered in the FAQ, but it doesn’t answer my question, hence why I am posting.
I am in The One Wizard challenge and this is my code so far;
while True:
enemy = hero.findNearestEnemy()
if enemy and hero.canCast("chain-lightning", enemy):
hero.cast("chain-lightning", enemy)
else:
hero.attack(enemy)
I get the “target is null”-problem because after I kill the first 3 enemies it takes awhile for the next to spawn, which means that my hero.attack(enemy) turns up null.
So, how do I fix this? How do I code so that it only attacks IF there are enemies, and if there aren’t any enemeis then it does something else?
I tried this;
while True:
enemy = hero.findNearestEnemy()
if enemy and hero.canCast("chain-lightning", enemy):
hero.cast("chain-lightning", enemy)
if not enemy:
hero.say("Waiting")
else:
hero.attack(enemy)
But I got the same issue. So, how do I solve this?
Edit: I solved the issue myself by changing the code into this -.
while True:
enemy = hero.findNearestEnemy()
if not enemy:
hero.say("Waiting")
else:
hero.attack(enemy)
if enemy and hero.canCast("chain-lightning", enemy):
hero.cast("chain-lightning", enemy)
But I don’t understand why it makes a difference - Can someone explain it to me?