Nearest Gold Coin


#1

Hi!
I’m trying to pick up only the closest gold coins.
Why does this not work?

coins = []
for item in hero.findItems():
    if item.value == 3:
        coins.append(item)
coin = hero.findNearest(coins)
hero.move(coin.pos)

#2

If coin not exist, than move method return error. So, you need to check it before move.

if coin:
    hero.move(coin.pos)

#3

RobAnybody has the answer. If there are no Gold Coins on the map your list will be empty. In which case there is no coin to take the coin.pos of. Hero.move(coin.pos) will be empty. And moving without a position returns an error.


#4

Thank you!
That helped!


#5

Thanks a lot for the explanation!