Nearest Gold Coin

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)
1 Like

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

if coin:
    hero.move(coin.pos)
2 Likes

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.

2 Likes

Thank you!
That helped!

1 Like

Thanks a lot for the explanation!

2 Likes