Sarven Shepherd -- strange idle (Lua)


#1
-- Use while loops to pick out the ogre
loop
    local enemies = self:findEnemies()
    local enemyIndex = 1
    -- Wrap this logic in a while loop to attack all enemies.
    while enemyIndex < #enemies do
        local enemy = enemies[enemyIndex] --use index numbers to select enemy
        local enemyIndex = enemyIndex + 1
        -- "!=" means "not equal to."
        if enemy.type ~= "sand-yak" then
            -- While the enemy's health is greater than 0, attack it!
            while enemy.health > 0 do
                self:attack(enemy)
            end
        end
    end
end

    -- Between waves, move back to the center.

So this is my code in Lua for https://codecombat.com/play/level/sarven-shepherd,
It’s working, without any errors, but character idles for few seconds on killing an enemy. I have no idea what causes the problem.

Also, I don’t know how to make my character “back to center” between waves, I know self:move(x,y), I just don’t know when is the “between waves” thingy.


#2

I don’t know Lua, but I can possibly help you.

You could detect if there are enemies, if there are none, then move to the middle.
I don’t know about the pausing of your character.


#3

As Enchanted suggested, when the enemies are gone the “Wave” is over so you’d just put the move after the while loop is finished. So something like:

loop
…while enemies
…do stuff
…move to center

As to the other problem, you only attack if number of enemies is greater than enemyIndex… what happens if there is only one enemy . . . . . . . you don’t attack . . . . until something else comes on the screen. change it to: index “<=” number of enemies, and your hero should stop waiting for more enemies…


#4

Thank you! Problem solved!