Sarven Treasure Error


#1

not sure why I’m getting this error. Obviously I’ve not learned how to properly define a variable. :disappointed:

Any and all help is appreciated.


#2

Look carefully at what the value of teleporter might be on line 12, and then look at what you’re trying do with that teleporter value. :slight_smile:

The error message says Cannot read property 'x' of undefined. What you are trying to read the property x from?


#3

What I thought it was doing in line 12 was refer to line 8 to get the nearest of an array of teleporters from line 7. I’m not sure why that doesn’t work. It says that teleporter is not defined but I thought I did define it in line 8 - the nearest of teleporters. Obviously my thinking is flawed. I just don’t understand why.


#4

Are you sure that’s what it’s saying? :slight_smile: I suspect the red highlighting in the code is a bit misleading, but to me, the error message doesn’t say this.

“Can’t read property ‘x’ of undefined”. Your code is accessing the property ‘x’ on what object?


#5

the only object it can be is teleporter. If this is so, then is it saying that it can’t read the x property of teleporter? and if that’s true, that means it’s not able to see the array of teleporters in findNearest. So how do I get it to find the nearest of the array?


#6

Let’s say teleporter happens to be {"x": 5, "y": 49}

What’s the value of teleporter.pos ?

What’s the value of teleporter.pos.x ?

What’s the value of teleporter.x ?


#7

OKAY! Got that part figured out. Thanks for your help. It is really appreciated. Now, character immediately starts collecting coins and when an ogre gets too close he heads for a teleporter. Yay! Unfortunately, he then just starts going in and out of the same teleporter and ignores the ogres and coins. Once the code reaches line 12 it just stays there even though the conditions are no longer true. Yes there are enemies but no, they are not < 10 in distance. Shouldn’t that be enough to make the code continue with the loop?

How do I get it to continue and not just stick there? Why is it staying there even though the conditions are no longer true?


#8

I still can’t get this to work properly. I was using the moveXY method in line 12, but once the character used the teleporter, he just kept going in and out of the same teleporter, completely ignoring the ogres and coins. Once it started processing line 12 it just got stuck there and would not continue, even though the conditions of the if statement were no longer true.

After reading this post by Harry The Wanderer I changed to the move(targetPos) method but just can’t get it to work at all. What the heck am I doing wrong?

while True:
    teleporters = [{"x":5, "y":49},{"x":5, "y":19},{"x":76, "y":51},{"x":77, "y":19}]
    teleporter = hero.findNearest(teleporters)
    telePos = teleporter
    coin = hero.findNearestItem()
    enemy = hero.findNearestEnemy()
    if enemy and hero.distanceTo(enemy) < 5:
        hero.move(telePos)
    if coin:
        hero.moveXY(coin.pos.x, coin.pos.y)

#9

Hello Munkey,

Here are some clues:

  • You should read the API docs for hero.move() and make sure you’re using it right.
  • Why would you need both telePos and teleporter?
  • Keep in mind that hero.move will only move to the teleporter until you’re more than 5 meters away (since distance is no longer < 5).

#10
  • What are API docs and where do I find them? (sorry, just never heard anything here called that before)
  • I put that in there after trying various other things. I didn’t really think it would resolve the issue but I was getting desperate.
  • I didn’t think of this. thanks.

The third item above is a real problem with these teleporters. I can’t use the moveXY() because it just gets stuck on that line and becomes an infinite loop. The character just keeps going in and out of the same teleporter, completely ignoring the coins and ogres. The code gets stuck and it can’t continue. The move() command doesn’t work effectively either because the character just takes a single step toward the teleporter and then goes back to collecting coins without entering the teleporter. This is not what is desired.

As an experiment, I added 22 lines of the hero.move(). Obviously this is horrible to do from a coding perspective but it now works the way I want it to. When an enemy gets too close, the character moves to the closest teleporter and when he comes out the other side he starts collecting coins again.

Is there some other way for me to achieve this?

if enemy and hero.distanceTo(enemy) < 15:
        hero.move({"x": teleporter.x, "y": teleporter.y})
        hero.move({"x": teleporter.x, "y": teleporter.y})
        hero.move({"x": teleporter.x, "y": teleporter.y})
        hero.move({"x": teleporter.x, "y": teleporter.y})
        hero.move({"x": teleporter.x, "y": teleporter.y})
        hero.move({"x": teleporter.x, "y": teleporter.y})
        hero.move({"x": teleporter.x, "y": teleporter.y})
        hero.move({"x": teleporter.x, "y": teleporter.y})
        hero.move({"x": teleporter.x, "y": teleporter.y})
        hero.move({"x": teleporter.x, "y": teleporter.y})
        hero.move({"x": teleporter.x, "y": teleporter.y})
        hero.move({"x": teleporter.x, "y": teleporter.y})
        hero.move({"x": teleporter.x, "y": teleporter.y})
        hero.move({"x": teleporter.x, "y": teleporter.y})
        hero.move({"x": teleporter.x, "y": teleporter.y})
        hero.move({"x": teleporter.x, "y": teleporter.y})
        hero.move({"x": teleporter.x, "y": teleporter.y})
        hero.move({"x": teleporter.x, "y": teleporter.y})
        hero.move({"x": teleporter.x, "y": teleporter.y})
        hero.move({"x": teleporter.x, "y": teleporter.y})
        hero.move({"x": teleporter.x, "y": teleporter.y})
        hero.move({"x": teleporter.x, "y": teleporter.y})


Any and all suggestions are appreciated. Thank you.


#11

try something like

while ( hero.distanceTo( telePos ) > 1 
    and hero.distanceTo( telePos ) < 50 ) :
            hero.move(telePos)

#12

FINALLY!!! I have seriously been working on this level for over a D@M WEEK.

@htrnblr: THANK YOU! I had to fiddle around with what you wrote a tiny bit but it got me on track and now it’s working just the way I want.

I wish I could post finished code here. It’s simple and works well. Whenever an ogre gets too close, character heads for a teleporter and as soon as he pops out the other side, he starts collecting coins again - until enemies get too close again.

PERFECT!