Chase Them Level Help

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())

1 Like

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.

1 Like

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

2 Likes

J_M_O_

Thank you for your help.

1 Like

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())

1 Like

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.

2 Likes

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

2 Likes

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

2 Likes

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

2 Likes

My pet says “can’t carry that” My hero is never cured.

/ The pet is your only hope.
function onSpawn(e) {
while(true) {
var enemy = pet.findNearestByType(“munchkin”);
if (enemy && pet.isReady(“chase”)) {
pet.chase(enemy);
}
// Find and fetch a “potion”:
var potion = pet.findNearestByType(“potion”);
if (potion) {
pet.fetch(“potion”);
}
}
}

// Assign “onSpawn” handler on the “spawn” event:
pet.on(“spawn”, onSpawn);Preformatted text

1 Like

Please learn to post your code correctly. The way it is now, we can’t see the structure. Help us help you. It’s very easy to do and just takes a tiny bit of effort. Please read this topic and format your code again correctly

1 Like

Taking a stab at it without proper formatting. I noticed that when you call the pet.fetch method you don’t use the variable potion that you created. Remove the quotations to use the variable and you might be good to go unless there are other formatting problems we can’t see now.

2 Likes

Brooksy125 - Thanks so much , that was super helpful and apparently exactly what was needed.

1 Like

I’m assuming this was meant for me. As a noob, I was both having problems understanding what I came here to ask for help for, and for your response.

I’ve edited my original text. I don’t really see the difference, but thanks for pointing out what little I could have done in order to format my code properly.

Were you able to point out something as helpful as Brooksy has then that would have been nicer.

1 Like

No, you put your code inside the backticks. Like this:

```

/ The pet is your only hope.
function onSpawn(e) {
while(true) {
var enemy = pet.findNearestByType(“munchkin”);
if (enemy && pet.isReady(“chase”)) {
pet.chase(enemy);
}
// Find and fetch a “potion”:
var potion = pet.findNearestByType(“potion”);
if (potion) {
pet.fetch(“potion”);
}
}
}

// Assign “onSpawn” handler on the “spawn” event:
pet.on(“spawn”, onSpawn);

```

and it will look like:


/ The pet is your only hope.
function onSpawn(e) {
    while(true) {
        var enemy = pet.findNearestByType("munchkin");
        if (enemy && pet.isReady("chase")) {
            pet.chase(enemy);
        }
        // Find and fetch a "potion":
        var potion = pet.findNearestByType("potion");
        if (potion) {
        pet.fetch("potion");
        }
    }
}

// Assign "onSpawn" handler on the "spawn" event:
pet.on("spawn", onSpawn);
1 Like

As chaboi said, you haven’t actually done it right yet, but let me explain why you have to do it.
The main difference between formatted and not formatted code is the indents. Normal text doesn’t keep the indents you have in code, without them it’s extremely hard to read (especially in Javascript) and for all we know your error could lie in your indentation, or placement of brackets.
Here’s an example of how non-formatted code can be confusing:

while(true) {
var enemy = hero.findNearestEnemy();
if(enemy) {
hero.attack(enemy);
if(enemy.health > 100 && hero.isReady(“bash”)) {
hero.bash(enemy);
}
}
}
This could be many different things (Well, there’s only one error free way to write it, but if it was free of errors, why would any one post it here?):

# this is the most obvious version (because it's (syntax) error free, although it's not a great program)
while(true) {
    var enemy = hero.findNearestEnemy();
    if(enemy) {
        hero.attack(enemy); 
        if(enemy.health > 100 && hero.isReady("bash")) { 
            hero.bash(enemy);
        }
    }
}

# This code makes more sense, but contains some errors:
while(true) {
    var enemy = hero.findNearestEnemy();
    if(enemy) {
        hero.attack(enemy);
    if(enemy.health > 100 && hero.isReady("bash")) { #what if there wasn't an enemy?
        hero.bash(enemy);
    } # this should be after "hero.attack(enemy)"
    }
}

So can you see what I’m saying here?
I do agree that sometimes one can help just by looking at unformatted code (like Brooksy), but it would be a lot nicer of you to try and make it easier for us. All we want to do is help you, that’s why were here.
One more thing: In future try and be more: a) polite (take alex2 as an example, if he doesn’t stop being rude, he might get muted) and b) more open to advice.
Thanks,
:lion: :lion: :lion:

2 Likes

Technically, I can see it in the editor of the post. :stuck_out_tongue:

1 Like

Je ne comprends pas, Chaboi. What do you mean?
:confused_lion: :confused_lion: :confused_lion:

1 Like

I meant that I can see the formatted text in the editor of the post.

1 Like

Yeah, he tried, but he still needs to put them in ‘’’

1 Like