Sarven Sun help with functions

Hello,

After a few days of struggling, I finally managed to finish Sarven Sun, however, my code is acting funny and I’m not sure what I’m doing wrong.

Running this code will succesully finish the levele, including the optional goal. But I have a function for my pet to fetch me potions. However, when the pet does that, my hero stops doing the action he’s doing until he gets the potion.

Can someone kindly take a look at my code and tell me how to run those actions in parallel? Or what am I doing wrong?

Thanks in advance.
(also, most likely my code it’s overly complicated, but, I’m VERY new to programming)

# To disable the fire-traps add the lowest trap.value to the highest value.
# Move to the white X and say the answer to Kitty the cougar.
# Defeat all the ogres if you dare.
# Once all ogres are defeated move to the red X.
# Look out for potions to boost your health.

whiteX = {'x':27, 'y':42}
redX = {'x':151 , 'y': 118}

def deactivateTrap():
    traps = hero.findHazards()
    trapIndex = 0
    minValue = 99999999
    maxValue = 0
    
    while trapIndex < len(traps):
        trap = traps[trapIndex]
        if trap.value < minValue:
            minValue = trap.value
        if trap.value > maxValue:
            maxValue = trap.value
        trapIndex += 1
    hero.say(minValue + maxValue)
    
def ligthning(target):
    if target:
        if hero.canCast("chain-lightning", target):
            hero.cast("chain-lightning", target)
    
def petPotion():
    item = hero.findNearestItem()
    if item and item.type == "potion" and hero.health <= (hero.maxHealth / 2):
        pet.fetch(item)
    
    

def fight():
    enemies = hero.findEnemies()
    weakest = None
    enemyIndex = 0
    maxiHealth = 999999
    
    while enemyIndex < len(enemies):
        enemy = enemies[enemyIndex]
        distance = hero.distanceTo(enemy)
        
        if enemy.health < maxiHealth:
            weakest = enemy
            maxiHealth = enemy.health
            enemyIndex += 1
            
        if enemy:
            if len(enemies) > 5:
                ligthning(enemy)
            else:
                hero.attack(enemy)
        enemyIndex +=1

    
hero.moveXY(27, 42)
deactivateTrap()
while True:
    hero.move(redX)
    fight()
    petPotion()

use pet.on("spawn", function) before the while loop instead of calling the function in your while loop because that will interrupt whatever the hero is doing.
Edit: The function will only execute once so add a while loop in the function if you need it.

Thanks for ytour response.

I added it at the end. But now the pet is not fetching it. :frowning:

hero.moveXY(27, 42)
deactivateTrap()

pet.on("spawn", petPotion)

while True:
    hero.move(redX)
    fight()

Did you put the contents of the function in a while loop?

I didn’t… I guess that makes sense… would it be a while True?

Let me try that! :smiley: thanks!

OMG! You’re a genius! That worked! Thank you! Truly appreciated!
This level took me like 4 days to complete… I was about to give up! Thank you so much!

1 Like

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