Error in Underground Business

I’m on the Underground Business level (in the desert), and I keep on getting these bizarre error messages that suggest replacing my code with hero.pos in the most bizarre places. My general experience is that this means I have a bug somewhere in my code, but I’m not catching it. I’ll post my code (using Naria) just so the community can decide if this is my coding or something with the gameplay.

def onSpawn(event):
    coin = pet.findNearestByType("gold-coin")
    # Send the pet to walk around the dungeon:
    pet.moveXY(coin.pos.x, coin.pos.y)
    # Don't forget to return it to the hero:
    pet.moveXY(hero.pos.x, hero.pos.y)

pet.on("spawn", onSpawn)

while True:

    hero.moveXY(23, 24)
    if hero.isReady("hide"):
        hero.hide()

    enemy = hero.findNearestEnemy()
    if enemy:
        distance = hero.distanceTo(enemy)
        if distance < 40 and distance > 20:
            if hero.isReady("throw"):
                hero.throw(enemy)
            elif hero.canCast("chain-lightning", enemy):
                hero.cast("chain-lightning", enemy)
            elif hero.isReady("envenom"):
                hero.envenom()
                hero.attack(enemy)
            else:
                hero.attack(enemy)
        else:
            hero.attack(enemy)

    if hero.gold > 299:
        hero.moveXY(50, 34)

This is a script with a bunch of if’s, but I don’t think of it as a spoiler - there’s no unique problem solving in it that’s not available in the guide to the level.

1 Like

What are you waiting from your code? As I see the pet will move to the nearest golden coin (if it can see it), then it moves to the hero. Once. It’s all.

1 Like

Yeah, you’re right; that’s a mistake on my part. But it still doesn’t explain why the game won’t take my code at all, like I mentioned in the question.

1 Like

Could you post a screenshot or text of the error? I suppose it’s because your pet doesn’t see coin so you can get pos of undefined.

1 Like

Strange. I opened up the editor, and threw in the loop for the pet, and the error message went away, and even when I tried to recreate it by taking out the loop again, it didn’t come back, meaning that I guess you were right @Bryukh , and that it had something to do with the lack of a loop in the pet script.

I’ll tell you what I don’t get, though. The error message (“Try hero.pos instead”) was always in the hero’s script, and at random places. I remember it telling me to change the ‘True’ in where True: to hero.pos, along with an enemy variable in hero.attack(enemy), and I know there were others. The error message did mention something about getting a null, but, unfortunately, I can’t recall it precisely.

Hope that helps, and if you could explain to me what went wrong, I’d really appreciate it.

1 Like

Aha! I got the error back!

(By accident, but maybe I’ll try and pretend it was on purpose.)

@Bryukh, it looks like you were right, though I still can’t figure out why 1) the editor suggests hero.pos as a good alternative, and 2) why it had been showing up in random places in the hero code.

1 Like

The system can only suggest, coin is undefined, so the most often case that hero has pos so the system can guess that you wanted.

Yep, it’s a problem with threads and it’s known bug. The error raises in onSpawn which is running not in the main thread so the system show the error like it’s in the main thread.

2 Likes

I also have an error but with a different code

# Accumulate 300 gold and escape from the dungeon.

def onSpawn(event):
    # Send the pet to walk around the dungeon:
    pet.moveXY(20, 58)
    pet.moveXY(71, 57)
    pet.moveXY(70, 11)
    pet.moveXY(20, 11)
    # Don't forget to return it to the hero:
    pet.moveXY(20, 34)
    pass

pet.on("spawn", onSpawn)

while True:
    # Guard peasants:
    while hero.gold < 300:
        enemy = hero.findNearestEnemy()
        if enemy:
            hero.attack(enemy)
    # When you have 300+ gold move to the red mark:
    hero.moveXY(50, 34)
    pass

the error is
code never finished its either really slow or has an infinite loop.

1 Like

Try not to put the while loop inside the while-true loop.

1 Like

@Hellenar nope now i just sit there doing nothing

got it

i needed a if hero.gold >= 300 instead of while hero.gold < 300

1 Like

Do anyone resolve my problem?

Sure

Did you look at my tip?

But i don’t understand :frowning: huu