Spinach Power Lua


#1

Hi community!
I got stuck on the Spinach Power level and after scratching my head for a day I have no idea what’s wrong with the code:

local potCount = 0
local enemy = hero:findNearest(hero:findEnemies())
while true do
        local item = hero:findNearest(hero:findItems())
        if item then
            hero:move(item.pos)
            local potCount = potCount + 1
            if potCount == 7 and enemy then
                hero:attack(enemy)
            end
        end
end

The problem is my hero instead of getting the 7 potions, poke one of the ogres and then resumes the potion collecting. It quite leaves me speechless since it seems she doesn’t care about the “if potCount == 7 and enemy then” line.

Cheers, Cthonia


#2

Your code first collect an item, then check the counter. As the result you attack once the enemy and your code start the whole cycle again.

You can break while-loop when counter >= 7 and start the new one with attack.
Or don’t collect potions if the counter >= 7.


#3

Ah, I understand. Thanks for the reply!