[SOLVED] Help with "The One Wizard" - C++

Hello!

I am having difficulty solving the stage “The One Wizard” in C++.

I am trying to make use of the code enemy.type == “ogre”;

Do I need to define enemy.type before I use it?

Error: Line 24: TypeError: Cannot read property “type” of null

Here’s my code

// Defeat as many ogres as you can.
// Use 'cast' and 'canCast' for spells.

int main() {
    while(true) {
        auto enemy = hero.findNearestEnemy();
       if (enemy) {
            if (hero.canCast("lightning-bolt")) {
            hero.cast("lightning-bolt", enemy);
        }
           if (hero.canCast("chain-lightning")) {
               hero.cast("chain-lightning", enemy);
            
        } 
            else {
            hero.attack(enemy);
            }
        }
        else {
            if (hero.canCast("regen")) {
                hero.cast("regen", hero);
            }
        }
        if (enemy.type == "ogre")  {
            hero.moveXY(6, 34);
        }
    }   
    return 0
}

Screenshot of the error and if somebody can explain what the error means I would be glad aswell! Just started out with coding and happy for all the information I can get :slight_smile:

Hi there!

No because it is a property of the enemy which can be accessed if there is an enemy

So the error here is saying, it cannot get a property of null, null means empty or none, so if it doesn’t have an enemy, it can’t get it’s property. So using an if-statement checking whether there is an enemy or not would solve the problem.

So here you put it, but down you didn’t

you would want to change this into if enemy and enemy.type equals to ogre

Hopefully that helps :slight_smile:

1 Like

Hello Aya!

Thanks for your help & explanation!

Ill test it as soon as i’m back on a computer!

Ill make sure to post the outcome :slight_smile:

2 Likes

This topic was automatically closed 12 hours after the last reply. New replies are no longer allowed.