Siege of StoneHold Level help (PYTHON)

Hi, I have been stuck on this level for quite some time and can’t seem to figure out what is wrong can someone help me?
Here is my code:

while True:
    enemy = hero.findNearestEnemy()
    distace = hero.distanceTo(enemy)
    flag = hero.findFlag()
    if distance < 20:
        if hero.isReady("cleave"):
            hero.cleave(enemy)
        if not hero.isReady("cleave") and hero.isReady("bash"):
            hero.bash(enemy)
        else:
            hero.attack(enemy)
            hero.attack(enemy)
    if not distance < 20:
        flagpos = flag.pos
        fx = flagpos.x
        fy = flagpos.y
        hero.moveXY(fx, fy)
        hero.pickUpFlag(flag)

A little error

It should be

        fx = flagpos.pos.x
        fy = flagpos.pos.y

Milton, you don’t need .pos.x because flagpos has already been defined as flag.pos.
I think the problem is here:

Typo in distace. Should be distance (with an n).
Danny

Wait… are you sure there’s even a flag when distance < 20? I hope there is a flag!

What do you mean? In human language

this part is if the distance is smaller than 20 define variables move to variables then pick up the flag.
Edit: now I understand what you’re saying.

So with the distance
It’s giving me an error that it is null why is this?

delete if distance > 20 replace it with

if enemy and hero.distanceTo(enemy) > 20

Still got the same error as shown in the picture above

You have to get rid of distance = hero.distanceTo(enemy). You can’t work out the distance if the enemy doesn’t exist.

@milton.jinich , I did say that here:

And @Giovanni_Busacca did change it. If you look at the screenshot, that error is resolved.
Danny

I know my typo was fixed but why is there a Line Error?

I changed by code a bit and here it is now.
Is there any problems?

while True:
    enemy = hero.findNearestEnemy()
    flag = hero.findFlag
    if enemy and hero.distanceTo(enemy) > 20:
        if hero.isReady("cleave"):
            hero.cleave(enemy)
        if not hero.isReady("cleave") and hero.isReady("bash"):
            hero.bash(enemy)
        else:
            hero.attack(enemy)
            hero.attack(enemy)
    if flag:
        hero.moveXY(pos.x, pos.y)
        flagpos = flag.pos
        fx = flagpos.x
        fy = flagpos.y
        hero.moveXY(fx, fy)
        hero.pickUpFlag(flag)

The area at fx = flagpos.x it gave me an error for some reason

it should be

flag = hero.findFlag()

Ok that was the solution but now with the correct code I noticed that when my hero used the Cleave ability all the enemies around him died and he was supposed to go attack the other’s but instead he just sat there with the statement “but it’s dead…” how can I fix this?

If that was the solution please mark it with the :white_check_mark:. The “but it’s dead…” thing isn’t preventable. It happens when an ally kills the enemy you where targeting.

It is preventable if you do enemy.health > 0:

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