# Collect 25 gold, and then tell Naria the total.
# Use break to stop collecting when totalGold >= 25.
totalGold = 0
while True:
coin = hero.findNearestItem()
if coin:
# Pick up the coin.
hero.move(coin.pos)
# Add the coin's value to totalGold.
# Get its value with: coin.value
totalGold += coin.value
pass
if totalGold >= 25:
# This breaks out of the loop to run code at the bottom.
# The loop ends, code after the loop will run.
break
# Done collecting gold!
hero.moveXY(58, 33)
# Go to Naria and say how much gold you collected.
hero.say(totalGold)
When using hero.move the character gets first coin right in front, then immediately breaks the loop, goes to the bottom XY coordinates and says “27” if I use Hero.moveXY and the full written coin.pos.x / y then it works.
I’m trying to determine why hero.move breaks it and especially where it’s getting a random “27” variable from.Preformatted text
If you only do this, totalGold will only count the coin.value, so that’s why you’re getting 27.
This line right here should be totalGold = coin.value + totalGold
While troubleshooting the other part I also saw a reference about totalGold = totalGold + coin.value but then also found a video using the one from mine and the code works fine only changing the hero.move(coin.pos) to hero.moveXY(coin.pos.x, coin.pos.y)
It does work with that change, but I’m pulling out hair trying figure out why that breaks the loop and gives a high number when I’ve only collected a gold value of 3
The loop is breaking at the first coin collected and a total gold shown as 3, let me know if you get the same results when you copy and paste my code. It’s in Sarven Desert 10lvls down the main path.
So, @Manticore412, I tried running your code and I found the issue, but it’s not something that should normally make your code not work. The only difference between our codes though was the exact same thing @Lydia_Song said:
This is your difference:
This what mine changed:
hero.moveXY(coin.pos.x, coin.pos.y)
It doesn’t make a lot of sense why this would change anything, because essentially they do the exact same thing. The only thing I can think of is that there might have be an issue internally, like when they were creating the level because they might have wanted you to only use hero.moveXY() in this level. Let me know if you have any further questions. Hope this helps! Grzmot