Help with Salted Earth

Hey all,
I’m having an issue with Salted Earth. I’m not sure if it’s the syntex but on the 7th line i’m getting an error ‘Typeerror: Cannot read property of ‘type’ of null’ for some reason.
here’s my code

# Ogres are attacking a nearby settlement!
# Be careful, though, for the ogres have sown the ground with poison.
# Gather coins and defeat the ogres, but avoid the burls and poison!

while True:
    enemy = hero.findNearestEnemy()
    if enemy.type == "munchkin" or enemy.type == "thrower":
        hero.attack(enemy)
    item = hero.findNearestItem()
    # Check the item type to make sure the hero doesn't pick up poison!
    # If the item's type is "gem" or "coin":
    pos = item.pos
    x = pos.x
    y = pos.y
    if item.type == "gem" or "coin":
        # Then move and pick it up:
        hero.moveXY(x, y)

Not sure what’s wrong

Try checking for the existence of items or enemies before checking type. You’re telling the code to check the type of something that may not exist, thus null.

Isn’t that what enemy = hero.findNearestEnemy() and item = hero.findNearestItem() is doing in the code?

No, those lines are defining your variables.

Well, you can’t just write

Without an if item:, because you can’t use item if you have no certification that an item is even there. Same with the

.

Got it, I fixed the issue. It shows the issue was line seven but it was actually my item checking that was wrong. I don’t know why is said the issue was on another line.

Thanks! I was able to figure it out. According to the code

If enemy.type == "munchkin" or enemy.type == "thrower":

was right but it said it was wrong until I fixed the item.type.

I’m confused as to what’s causing an error here:

// Ogres are attacking a nearby settlement!
// Be careful, though, for the ogres have sown the ground with poison.
// Gather coins and defeat the ogres, but avoid the burls and poison!

while(true) {
    var enemy = hero.findNearestEnemy();
    if(enemy.type == "munchkin" || enemy.type == "thrower") {
        hero.attack(enemy);
    }
    var item = hero.findNearestItem();
    // Check the item type to make sure the hero doesn't pick up poison!
    // If the item's type is "gem" or "coin":
    if (item.type == "gem" || item.type == "coin") {
        // Then move and pick it up:
        hero.moveXY(item.x item.y);
    }
}

Try (item.pos.x, item.pos.y)

2 Likes
while(true) {
    var enemy = hero.findNearestEnemy();
    if(enemy.type == "munchkin" || enemy.type == "thrower") {
        hero.attack(enemy);
    }
    var item = hero.findNearestItem();
    // Check the item type to make sure the hero doesn't pick up poison!
    // If the item's type is "gem" or "coin":
    if (item.type == "gem" || item.type == "coin") {
        // Then move and pick it up:
        hero.moveXY(item.pos.x item.pos.y);
    }
}

image

you forgot to put a comma in the middle

2 Likes