"TypeError: Cannot read property 'pos' of undefined" Sarven Brawl using Python


#1
# Stay alive for two minutes.
# If you win, it gets harder (and more rewarding).
# If you lose, you must wait a day before you can resubmit.
# Remember, each submission gets a new random seed.
def attack(target):
    if target and hero.isPathClear(hero.pos, target.pos)==True:
        if hero.isReady("cleave") and (hero.distanceTo(target) < 3):
            hero.cleave(target)
        hero.attack(target)
    else:
        hero.shield()
    pass

def dodgeProjectile(missile):
    
    if missile:
        a=0
        if missile[a].pos.x == hero.pos.x:
            if hero.isPathClear(hero.pos.x+3, hero.pos.y):
                hero.moveXY(hero.pos.x+3,hero.pos.y)
            if hero.isPathClear(hero.pos.x-3, hero.pos.y):
                hero.moveXY(hero.pos.x-3,hero.pos.y)
        if missle[a].pos.y == hero.pos.y:
            if hero.isPathClear(hero.pos.x, hero.pos.y+3):
                hero.moveXY(hero.pos.x,hero.pos.y+3)
            if hero.isPathClear(hero.pos.x, hero.pos.y-3):
                hero.moveXY(hero.pos.x,hero.pos.y-3)
        a=a+1
    pass

enemy = hero.findNearestEnemy()
projectile = hero.findEnemyMissiles()

while True:
    attack(enemy)
    dodgeProjectile(projectile)

Sorry for the spaghetti code. Wondering why I’m getting this error
"Fix Your Code:
Try hero.pos
TypeError: Cannot read property ‘pos’ of undefined"

Any help would be appreciated!


#2

Try defining enemy and projectile in the while-true loop so it doesn’t just run once. :slightly_smiling_face:


#3

Just tried it, and no luck! Same error as before.


#4

Do you have a sense stone of any sort equipped (enables you to identify pos?)


#5

Yes, I have the Sapphire Sense Stone. Which grants pos, target, and targetPos. Among other things.


#6

I had to look through your code one more time. Sorry I missed it…

Just a simple typo. Don’t worry, it happens to everyone once in a while. It’s perfectly normal :slightly_smiling_face:
Your code is correct otherwise; that’s why I got confused :joy:


#7

Okay, so this was a bit of a mess. I noticed the typo, and changed it, no dice. After looking some more at it, I realized I was using the isPathClear function incorrectly, since it needed to check a path between two XY coordinates. In my original code I had it as a simple XY coordinate, but I need a start and stop point. So, I changed it to this:

def dodgeProjectile(missile):
    if missile:
        a=0
        if missile[a].pos.y == hero.pos.y:
            if hero.isPathClear(hero.pos,{hero.pos.x+3,hero.pos.y}):
                hero.moveXY(hero.pos.x+3,hero.pos.y)
            if hero.isPathClear(hero.pos,{hero.pos.x-3,hero.pos.y}):
                hero.moveXY(hero.pos.x-3,hero.pos.y)
        if missile[a].pos.x == hero.pos.x:
            if hero.isPathClear(hero.pos,{hero.pos.x,hero.pos.y+3}):
                hero.moveXY(hero.pos.x,hero.pos.y+3)
            if hero.isPathClear(hero.pos,{hero.pos.x,hero.pos.y-3}):
                hero.moveXY(hero.pos.x,hero.pos.y-3)
        a=a+1
    pass

All of the rest of my code is the same, and this section seems to be fine from what I can see. I had to change my isPathClear coordinates, and put the second set into brackets because parenthesis didn’t work for some reason.

Only now I’m getting a completely different error:
"Don’t know how to transform: Set"
I have no idea what that means unfortunately.


#8

You’re using those curly bracket thingies (or this: {}), which are used for sets. You can use a variable for hero.pos.x,hero.pos.y+3 and implement that into your code (avoid using the curly brackets).


#9

I could be wrong, but I think IsPathClear needs the curly brackets (i.e., it takes sets), but I think it also needs you to key in the x and y, like: {"x": hero.pos.x + 3, "y": hero.pos.y}

I think that’s also true for the first parameter, though, so instead of banking on hero.pos, I think you need to hard code in a set where you set x as hero.pos.x and y as hero.pos.y, like before, with the quotation marks and colons. At least that’s what I’ve been doing when I use isPathClear, but maybe there’s a shorter way…