[SOLVED] Sarven Treasure - Python

I am trying to complete this level by having my character just run and pick up gold at first and then start fighting. All during this time I wan’t to be able to us a flag to tell my character to go to a teleporter. When I run the code it says "Hard execution limit of 3000000 reached? Any help or suggestions would be great. Thank you.

loop:
    items = self.findItems()
    item = self.findNearest(items)
    enemies = self.findEnemies()
    enemy = self.findNearest(enemies)
    green = self.findFlag("green")
    black = self.findFlag("black")
    violet = self.findFlag("violet")
    flags = self.findFlags()
    
    while self.health > 1000:    
        if item:
            self.moveXY(item.pos.x, item.pos.y)
        elif flags:
            if green:
                self.pickUpFlag("green")
                pass
    while self.health < 1000 and self.health > 500:
         if enemy:
            if self.distanceTo(enemy) < 10:
                if self.isReady("cleave"):
                    self.cleave(enemy)
                else:
                    continue
        elif item:
            self.moveXY(item.pos.x, item.pos.y)
        elif flag:
            if green:
                self.pickUpFlag("green")
                pass

what do you have
speed ring and softened leather boots are recommended
and also any good sword at least 200 damage

That means you have an infinite loop somewhere. Can you see any such loops in your code?
You might want to replace the parts with while self.health with if and else if.

You must update your world view (search for items, enemies, flags … inside each while loop
Otherwise will be stuck in the first while loop, you’ll pick the only item you searched for, and the do nothing until you run out of instructions.

An empty while loop will be executed a million of times in less than a fraction of a second.

OK thank you very much!

hey I know that u made this a while back but

flags = self.findFlags()
is not a command u need to remove the ‘s’ on the end of flags to be able to run the command

Howdy and welcome to the forum! Yes, it looks like Jacob has been inactive since he posted this.

Anyway, the statement he posted is actually correct, except that ‘self’ has been replaced with ‘hero’:
image
This method will find all flags of any color.

Otherwise, you can specify a color by:
hero.findFlag(“green”)

can someone please help me with my code

while True:
    
    items = self.findItems()
    item = self.findNearest(items)
    enemies = self.findEnemies()
    enemy = self.findNearest(enemies)
    green = self.findFlag("green")
    black = self.findFlag("black")
    violet = self.findFlag("violet")
    flags = self.findFlags()
    
    while self.health > 1000:    
        if item:
            hero.moveXY(item.pos.x, item.pos.y)
        elif flags:
            if green:
                self.pickUpFlag("green")
                pass
    while self.health < 1000 and self.health > 500:
        if enemy:
            if self.distanceTo(enemy) < 10:
                if self.isReady("cleave"):
                    self.cleave(enemy)
                else:
                    continue
        elif item:
            hero.moveXY(item.pos.x, item.pos.y)
        elif flag:
            if green:
                self.pickUpFlag("green")
                pass

it stops after getting two coin

Hmm you’re using self. Is that your code or have you copied it from somewhere else? If so please write some of your own.
Danny

ok

[en_US.composer.my_button_text]

oh and btw can you use self in python? because idk if u can or not

now it only collects 3 and doesn’t move after that

As part of a game engine upgrade/overhaul back around 2017, ‘self’ was replaced by ‘hero’. It will still work, but is slower and can be problematic.A few other commands/methods, like ‘loop’, were also replaced.

oh ok thank you

[en_US.composer.my_button_text]

i tried killing the enemies which worked but after that it stops this is my code

while True:
    enemy = hero.findNearestEnemy()
    item = hero.findNearestItem()
    items = hero.findItems()
    if item and item.value == 3 or item.value == 2:
        hero.moveXY(item.pos.x, item.pos.y)
    if enemy:
        hero.attack(enemy)
    

Try replacing if by else if.

ok thank you! (20 char)