Siege of StoneHold Hero Does Nothing (Python)

Hello,
I do not understand why my code does not run. The hero just sits there and preforms no action, not even shield.
Is the IDE not powerful enough to evaluate the and command or the various elifs?

enemy = hero.findNearestEnemy()
flag = hero.findFlag("green")
ready = hero.isReady("cleave")

while True:
    if enemy and ready :
        hero.cleave(enemy)
    elif enemy:
        hero.attack(enemy)
    elif flag:
        hero.findFlag("green")
    elif hero.health < 200: 
        hero.moveXY(20, 69)
    else:
        hero.shield()
    
  • enemy – You only search for an enemy once.
    You should search for enemies continuously.
    (No enemy found in the beginning means the lines if enemy and ready: and elif enemy: always evaluate to False)

  • flag – In the while True: loop
    The line, elif flag:
    Maybe you meant elif not flag:? (and you also need to move to the flag and pick it up)
    (No flag found in the beginning means that elif flag: will always evaluate to False)

  • ready – You never update this variable inside the while True: loop.
    Instead of if enemy and ready:, you can use if enemy and hero.isReady("cleave"):
    Then, you won’t need the ready variable anymore.
    (In your code right now, ready will always be True)

So, here’s your code again, but with comments about how it’s working:

enemy = hero.findNearestEnemy()    # you should put this in the loop so it can update continuously
flag = hero.findFlag("green")      # same with this
ready = hero.isReady("cleave")     # and this

while True:
    if enemy and ready :       # this always evaluates to (false and true) = false
        hero.cleave(enemy)         # (this code doesn't run)
    
    elif enemy:                # this always evaluates to false
        hero.attack(enemy)         # (this code doesn't run)
    
    elif flag:                 # this always evaluates to false
        hero.findFlag("green")     # (this code doesn't run)
    
    elif hero.health < 200:    # this works as expected
        hero.moveXY(20, 69)
    
    else:                      # and this is why you've noticed
        hero.shield()          # your hero shielding all the time

So one issue I found. The hero.health never gets invoked because there are so many enemies. How can I set it so it breaks out and goes to the health if it goes below 200?

That shouldn’t be the reason why that if statement fails. The 3 conditional checks before it always fail (except if you have the infinite range/see-through-walls glasses).

Fix the other issues first and worry about refilling on health later.
Really, I don’t see anything wrong with elif hero.health < 200:.

Hi everyone!

I used this code on “Mind the trap”, but doesn’t work on “siege of stonehold”…
my hero doesn’t move.

while True:
flag = hero.findFlag()
enemy = hero.findNearestEnemy()
distance = hero.distanceTo(enemy)

if flag:
    # Pick up the flag.
    hero.pickUpFlag(flag)
    hero.say("I should pick up the flag.")
if enemy and distance < 10 :
    # Only attack if the enemy distance is < 10 meters
    hero.attack(enemy)
while True:
    flag = hero.findFlag()
    enemy = hero.findNearestEnemy()
    distance = hero.distanceTo(enemy)
    
    if flag:
        # Pick up the flag.
        hero.pickUpFlag(flag)
        hero.say("I should pick up the flag.")
    if enemy and distance < 10 :
        # Only attack if the enemy distance is < 10 meters
        hero.attack(enemy)

Screen Shot 2020-01-03 at 09.28.49

Hello, welcome to the forum. We are glad to help.

Earlier levels may have always had an enemy on the map so you didn’t run into this problem. Before you can check the distance to an enemy, you need to make sure that there is an enemy to find the distance.

    enemy = hero.findNearestEnemy()
# what can you add here to check if there is an enemy before looking for the distance
    distance = hero.distanceTo(enemy)

Thank you so much brooksy I wouldn’t have found it without you.