[SOLVED]Plz help with sarven-siege

can’t get hero to build solider after collecting 20 coins, it would just collect up to 27 and stop
litte help would be very kind

def collectCoin():
    coins = hero.findItems()
    bestCoin = 0
    bestValue = 0
    for coin in coins:
        value = coin.value/hero.distanceTo(coin)
        if value > bestValue:
            bestValue = value
            bestCoin = coin
    if bestCoin:
        hero.moveXY(bestCoin.pos.x, bestCoin.pos.y)
        
        
while True:
    coin = hero.findNearestItem()
    gold = hero.gold
    while gold <= 20:
        collectCoin()
        break


    
while hero.gold >= 20:
    arrowTower=self.findByType("arrow-tower")
    nearestTower=self.findNearest(hero.findByType("arrow-tower"))
    if nearestTower:
        hero.moveXY(nearestTower.pos.x + 5, nearestTower.pos.y)
        break

Instead of your

while hero.gold > 20, 
#use 
if hero.gold > 20

Because if questions it, while says that while it is at it, and it is not.

Thank you guys, I’ll try That asap :slight_smile:

solved it, thanks a lot :slight_smile:

1 Like

Good job, you did it by yourslef!

some one plz help me this is my code

def collectCoin():
    coins = hero.findItems()
    bestCoin = 0
    bestValue = 0
    for coin in coins:
        value = coin.value/hero.distanceTo(coin)
        if value > bestValue:
            bestValue = value
            bestCoin = coin
    if bestCoin:
        hero.moveXY(bestCoin.pos.x, bestCoin.pos.y)
        
        
while True:
    coin = hero.findNearestItem()
    gold = hero.gold
    if gold <= 20:
        collectCoin()
        break


    
if hero.gold >= 20:
    arrowTower=self.findByType("arrow-tower")
    nearestTower=self.findNearest(hero.findByType("arrow-tower"))
    if nearestTower:
        hero.moveXY(nearestTower.pos.x + 5, nearestTower.pos.y)
        break

and also it would just stay still after 5 coins

Don’t you only want to break when you’ve got 20 gold?
Also I think you should move in the while true loop not in the function. Instead use return to get the variable “bestCoin” from the function to the while True loop.
Danny

like this?

def collectCoin():
    coins = hero.findItems()
    bestCoin = 0
    bestValue = 0
    for coin in coins:
        value = coin.value/hero.distanceTo(coin)
        if value > bestValue:
            bestValue = value
            bestCoin = coin
    if bestCoin:
        hero.moveXY(bestCoin.pos.x, bestCoin.pos.y)
        
        
while True:
    coin = hero.findNearestItem()
    gold = hero.gold
    if gold <= 20:
        collectCoin()
    break


    
if hero.gold >= 20:
    arrowTower=self.findByType("arrow-tower")
    nearestTower=self.findNearest(hero.findByType("arrow-tower"))
    if nearestTower:
        hero.moveXY(nearestTower.pos.x + 5, nearestTower.pos.y)
        break

No, only break if you do have 20 gold.
Also you might want to do something else to get the soldiers. Maybe check if you have 20 gold, then if you do just use moveXY inside the loop. If you break you’ll have to write your code over and over again.
Danny

ummmmm i am very confused? can you show me a example of that?

and also it just collect 25 coins and then stays still idk why?

Because the while loop has stopped.
I thnk break should be used sparingly. It means you exit out of the while True loop forever.
It would be better to have one while True loop, call the function at the start then check if you have enough gold and move using hero.moveXY() inside the loop. There’s no need to break out.
Danny