# Grab as much gold as you can, then retreat to your base and wall it off!
while hero.time < 20:
# Collect coins
hero.say("I should pick up a coin")
while hero.gold < 60:
nearest = hero.findNearest(hero.findItems())
hero.move(hero.pos)
while hero.pos.x > 16:
if hero>60:
# Retreat behind the fence
hero.moveXY(16, 38)
# Build a fence to keep the ogres out.
hero.buildXY("fence", 21, 38)
# Grab as much gold as you can, then retreat to your base and wall it off!
while hero.time < 20:
# Collect coins
hero.say("I should pick up a coin")
while hero.pos.x > 16:
if hero.gold > 60:
# Retreat behind the fence
hero.moveXY(16, 38)
# Build a fence to keep the ogres out.
hero.buildXY("fence", 21, 38)
At some point, you may collect all the coins your hero can see and there will not be a nearest item. When that happens, you hero can’t move to the nearest.pos and this error occurs. Simply add an if statement before the hero.move() command to ensure there is a value in the nearest variable before commanding the hero to move to that location.