[SOLVED] Doom Glade Little Code Error

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.

please check this topic out @ShiniG [Essentials] How To Post/Format Your Code Correctly

and welcome to the discourse

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

you have to use can cast @ShiniG

like this hero.canCast(“chain-lightning”, enemy)

The hero can only attack if there is an enemy, you’ve already defined enemy as a variable, so replace hero.findNearestEnemy() with enemy.

Replace the elif with if
and put this:

inside the if enemy loop.
And put this:

As an else loop at the bottom of the if enemy loop.
Lydia

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 ?

hmm maybe change the last else to if and the elif to if and add an else ?

Oh I meant

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

Lydia

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)
1 Like

Did you pass the level?
Lydia

Yes, with more health now.

Congrats! Please remove any solutions and be sure to check the tick mark next to someone’s comment that helped you the most!
Lydia

Nice adjustment @ShiniG!

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.