Play Home Contribute Github Join Us! Discourse Staff Members Github Contribution Guides Team

The Trials - Infinite Loop using while (or if) enemies and while (or if) items

So I ust reache dthe trial level in the desert, but it keeps lagging and when I refresh I keep getting the infinite loop notification, and I can’t seem to find it. Here is the code

return  #Commented out to stop infinite loop.
return  #Commented out to stop infinite loop.
# This level is intended to be for advanced players. The solution should be pretty complex with a lot of moving parts. It might also be a bit of a gear check unless you use creative methods.
# You need to make your way to the first trial (Oasis of Marr) defeating enemies along the way. When you reach it, pick all the mushrooms to trigger the trial to begin. If you survive the onslaught, make your way to the next Oasis for the second trial, then the Temple. When all trials are complete you will have to face the final boss. Good luck!
# HINT: Glasses with a high visual range help tremendously on this level so buy the best you can get.
# HINT: the unit 'type' for the oasis guardians is 'oasis-guardian'
def attack():
    enemies = hero.findEnemies()
    while enemies:
        enemy = hero.findNearest(enemies)
        while enemy.health > 0:
            hero.attack(enemy)

def pickUp():
    items = hero.findItems()
    while items:
        item = hero.findNearest(items)
        hero.moveXY(item.pos.x, item.pos.y)
            
            
attack()
hero.moveXY(127, 21)
pickUp()
hero.moveXY(27, 30)
attack()
hero.moveXY(8, 80)
hero.moveXY(15, 114)
pickUp()
attack()
hero.moveXY(39, 129)
attack()
hero.moveXY(97, 127)
hero.moveXY(111, 117)
pickUp()
attack()
hero.moveXY(60, 120)
hero.moveXY(39, 92)
hero.moveXY(67, 71)
hero.attack(enemy)

Oh and here is my equipment:
image

Instead of finding items and then finding the nearest item, find the nearest item an dmove to it if it exists.

hero.findEnemies() Quiz?

I thought of that, but then I’ll have to change the code to a while true loop and flags instead, is that better?

You could change it to flags and a while true loop if you wanted.

so I tried what is in the post that you linked, and it still infinite loops for some reason, I even removed all the code and tried running the function once and still it lagged and stopped

you don’t really ned this. just findnearestitem and if item wihtout the s

def attack():
    enemies = hero.findEnemies()
    while len(enemies):
        enemy = hero.findNearest(enemies)
        while enemy.health > 0:
            hero.attack(enemy)
        enemies = hero.findEnemies()

you must update somehow enemies inside the while loop
But using while enemies and while items is a path to disaster and this error is so common and non understood.
Absolutely the same applies for if enemies and if items

@thebagel @xython

thanks both of you I used a mix of oth of your tips it’s working now I’ll implement the flags and it should work, can I add more than one soloution?