Hi everyone, that’s my first topic, so I’m sorry if I have done something wrong. I’m having an issue with this level, said I have successfully passed. Yet, still have an error with my code.
Below is my code.
# An ogre army approaches. Use flags to win the battle!
while True:
enemy = hero.findNearestEnemy()
if hero.isReady("chain-lightning"):
hero.cast("chain-lightning", hero.findNearestEnemy())
elif hero.isReady("drain-life"):
hero.cast("drain-life", hero.findNearestEnemy())
elif enemy:
hero.attack(enemy)
And the error message is “Line 5: ArgumentError: cast’s argument target should have type object, but got null. You need something to cast upon.”
I tried to use enemy variable instead of using findNearestEnemy() all the time, but I got the same error.
Can someone help me figure out what’s wrong with my code? Big thanks afore.
This error happens when your hero is trying to cast a spell on the enemy without verifying if there is an enemy. Eventually you kill all the enemies, yet your code still wants to cast a spell against the nearest enemy. When it can’t find an enemy, you get that error. You always want to check for an enemy before attacking them.
One way to bundle the code together is checking for an enemy before all the different spells and attacks. And when you call the special attacks, use the variable name you used when checking if there is an enemy instead of looking for the enemy in the cast function.
enemy = hero.findNearestEnemy()
if enemy:
special attack 1 - check to see if ready before attack
special attack 2 - check to see if ready before attack
else regular attack
Thank you so much for the replies. I have tried to do after read what @brooksy125 told.
while True:
enemy = hero.findNearestEnemy()
if enemy and hero.canCast("chain-lightning", enemy):
hero.cast("chain-lightning", enemy)
elif enemy and hero.canCast("drain-life", enemy):
hero.cast("drain-life", enemy)
else :
hero.attack(enemy)
Worked pretty well now.
Not forgetting to say thank you for the advice and kindness milton.jinich
Also big thanks for the explanation and the advice @Lydia_Song I don’t know if I got right but I was supposed to replace the elif with another if ?
while True:
enemy = hero.findNearestEnemy()
if enemy:
#check if you can cast your spell
#cast spell
#check if you can cast your other spell
#cast spell
else:
#attack enemy
Thank you again for your explanation. I have nested the if statements and I could finish better this level.
while True:
enemy = hero.findNearestEnemy()
if enemy:
if hero.canCast("chain-lightning", enemy):
hero.cast("chain-lightning", enemy)
if hero.canCast("drain-life", enemy):
hero.cast("drain-life", enemy)
else:
hero.attack(enemy)
One last note, you could have left it as “if, elif and else” for the different attacks. It helps prioritize which one you want to run by only running one of these codes depending on which ones are available at that time.
Right now it will always check for both chain-lightning and drain-life, sometimes that is what you want while other times you want to pick the first one that is available with the “if, elif and else”. There are a few that kind of conflict with each other since some attacks take some time to use while trying to call a more priority attack. Little details that will help later on.