Chase Them Level Help


#1

When I click on the potion it does not give me it’s name like the enemies or gems do.
My code isn’t working I would appreciate any help. It is LUA

-- Defeat the ogres and cure the hero.

-- The pet is your only hope.
local function onSpawn() 
    while true do
        local enemy = pet:findNearestByType("munchkin")
        if (enemy and pet:isReady("chase")) then
            pet:chase(enemy)
        end
        -- Find and fetch a "potion":
        local potion = pet:findNearestByType("potion")
        if potion then
            pet:fetch(potion)
        end
    end
end

-- Assign "onSpawn" handler on the "spawn" event:
pet:on("spawn", onSpawn())


#2

Pretty sure it is a bug with the level. If I comment out the potion part the code runs. The game is not showing name for potion so my pet can’t find one.


#3

Could you say your nickname in Codecombat, I will try to research the problem.


#4

J_M_O_

Thank you for your help.


#5

Also, everytime I go back to this level there is a single return statement before my function which causes my to get an error message while loading into the level saying that there is an infinite loop

return  --Commented out to stop infinite loop.

-- Defeat the ogres and cure the hero.

-- The pet is your only hope.
local function onSpawn() 
    while true do
        local enemy = pet:findNearestByType("munchkin")
        if (enemy and pet:isReady("chase")) then
            pet:chase(enemy)
        end
       -- pet:moveXY(13,60)
        --pet:moveXY(hero.pos.x,hero.pos.y)
        -- Find and fetch a "potion":
        
        local potion = pet:findNearestByType("potion")
        if potion then
            pet:fetch(potion)
        end
        
    end
end

-- Assign "onSpawn" handler on the "spawn" event:
pet:on("spawn", onSpawn())


#6

It works now.

originally I had

pet:on("spawn", onSpawn())

now with

pet:on("spawn", onSpawn)

it works. I tried both ways several times before originally posting though.


#7

it should be pet.onSpawn("spawn", onSpawn). The second argument should be a function, onSpawn() is the function result. For your function onSpawn() == null.


#8

I think I get what you are saying. I can do it two ways?
Either
pet:on(“spawn”, onSpawn)
or
pet:onSpawn(“spawn”, onSpawn())


#9

You can use the second option in the case if your onSpawn() returns another function.