Play Home Contribute Github Join Us! Discourse Staff Members Github Contribution Guides Team

Pet.Fetch() not returning with potion

During the KithGard Brawl, my pet won’t return to me with the potion. It worked on the previous level so I’m not sure what circumstances changed that prevent him from returning. I had the issue one other time and switched to the onspawn approach I found in the forum @Deadpool198. Now it doesn’t work either and the pet runs to the corner with the potion. The only thing I can think of is that my hero is invisible. Does that prevent the pet from finding the hero?

def onSpawn():
    while True:
        potion = pet.findNearestByType("potion")
        if potion:
            pet.fetch(potion)

while True:
    pet.on("spawn",onSpawn)

Fetch%20problem
Similar post about problem

It definitely seems to be linked to the hero being invisible. I removed the invisibility code and had no problems. I changed the timing of the invisibility and had the pet returning and then run past me when I went invisible. When I go invisible when the pet is in fetch mode, it won’t return.

I even added another line to the “spawn” code to have the pet move to me, but I couldn’t get the potion from it.

if potion:
            pet.fetch(potion)
            pet.moveXY(hero.pos.x, hero.pos.y)

Fetch%20return

It’s an error to put pet.on(“spawn”,onSpawn) in a while loop

# while True: 
pet.on("spawn",onSpawn)
while True:
    # main loop code  here

edit:
you can debug from the console with code similar to:

    pot = hero.findByType('potion')[0]
    if pot:
        console.log('pot: ' + pot.pos + ' hero: ' + hero.pos + ' pet: ' + pet.pos + ' hide: ' + hero.hasEffect("hide"))

You are right, the pet doesn’t see the hero when he’s invisible. This doesn’t work:

def onSpawn():
    while True:
        potion = pet.findNearestByType("potion")
        if potion and hero.hasEffect("hide"):
            pet.fetch(potion)

Thanks for the note about not needing the While True considering it didn’t create an error or an infinite loop.

On a side note, where can I learn more about using the console for debugging? I saw the short intro on GitHub about the console, is there more? What all can it show and what is the best method to see what you want? I’ve just been using hero.say() for any debugging.

When there’s an error in the level, it’ll return the reason that it has the error in the console. Also, most artisans will use console.log() to debug instead of hero.say(). If you come across any broken levels, it’s recommended to copy/paste the error message with the report. I really want to add more to the guide, but I can’t find time to write it.

There is no error message, my pet just won’t bring the potion to me when I’m invisible and I don’t know how to work around that. I’m not sure if I just need to write my code to prevent the two from happening (invisible and pet.fetch) or if the pet should be able to find me regardless.

Either way, I’d like to learn how to use the console.log() option more to see what is really happening with the code. Thanks.

In this case, it’s just a problem with your code. I think console.log() is only used for debugging, when creating your own levels. I use it mainly to see if the conditions are written correctly. Examples are:

if @hero.pos.x>@target.pos.x
    console.log("Working")
else
    console.log("Not working")

Anyway, console.log won’t be used too much.

1 Like

I’m perplexed…
if @hero.pos.x>@target.pos.x
console.log(“Working”)
What language is this? What is the meaning of @ ?

It’s cofeescript, and it’s used to code the referee in the level editor

If it’s coffee script maybe the code is:

if @pos.x>target.pos.x
    console.log("Working")
else
    console.log("Not working")

No, in the level editor you have to define @hero.pos.x. Unless you state it like this:

@pos.x=@hero.pos.x

Added a check for the potion and invisibility to make sure they don’t run at the same time. The explanation for the invisibility spell only mentions that the hero would be invisible to any enemy. I wasn’t sure if it was intentional that the pet can’t see me either when he fetches. He can still find me otherwise even when I’m invisible.

I think that’s the point of being invisible. After all, being invisible means you can’t be seen, so why should your allies be able to see you?

I don’t disagree, it would just help to have that explained in the properties so it is understood correctly. Currently the property specifies the hero is invisible to “any enemy”. Since my pet doesn’t fall under that category am I wrong to think he would still be able to see me?

Yeah, I do agree that the description might be misleading, but I won’t consider my pet a “friend”

Good to know. Thanks.