Why do debug tell me I use a null value


#21

Well, this Idea is qiute interesting as conception, I use something like that, but with vectors, cause it’s much more easier. But looks like you have a very long journey before getting to vectors (it’s on Kelvintaph Glacier location).


#22

@Alexbrand
Are you using something like that?

friend = hero.findFriends()[0]
while hero.distanceTo(friend.pos)>10 :
    hero.move(friend.pos)

another version:

while True:
    if hero.distanceTo(friend.pos)>10 :
        hero.move(friend.pos)
    else:
        break

#23

No, it’s like:

def MoveTo():
    rightPoint = Vectоr(enemy.pos.x, enemy.pos.y)
    selfPOint = Vectоr(self.pos.x, self.pos.y)
    goal = Vector.subtrасt(rightPoint, selfPOint)
    goal = Vector.normalize(goal)
    goal = Vector.multiply(goal, 1)
    if enemy and enemy.health > 0:
        range = self.distanceTo(enemy)
        if enemy and range > 31:
            moveToPos = Vector.add(hero.pos, goаI)
            hero.move(movetoPos)
        else:
            do something

#24

Thanks @Alexbrand !
@MadMakII
rough implementation of your idea, tested and working one.
/ I don’t really understand recursion :wink: /

maTarget = hero.findFriends()[0]
while hero.distanceTo(maTarget)>10:
    midX = (hero.pos.x + maTarget.pos.x)/2
    midY = (hero.pos.y + maTarget.pos.y)/2
    hero.moveXY(midX,midY)
    # hero.move(Vector(midX,midY))  # better with vectors
    maTarget = hero.findFriends()[0]

#25

used same code, it works fine for me.

but here some catch, let say some other code execute before your function, like an attack, after that hero execute then GetCloser, see the mob,

while the hero moves, the monster die.

on the second instance of GetCloser the target is null and it bugs.

So whats best way to handle that?
There is no error handling available in codecombat

I am not 100% sure
but, I think you can use something like this:

If maTarget == None: 
    return

which will end the function.


#26

@Gabriel_Dupuis :

If maTarget == None:
    return

is valid but None is already a valid false value (I don’t like to compare two boolean values). you can check it with this code :

if None :
    hero.say("the result is true")
else :
    hero.say("the result is false")

meaning your code does the same as this one :

if maTarget :
    # do what you need with existing maTarget
else :
    # do what you need if maTarget does not exist

speaking with you all make me think that maybe I have to add at the end :

else :
    return

but I thought it had no use really. I’ll try it tomorrow as far as I will soon leave for work.
@Gabriel_Dupuis if by any chance your main language is French we could better use it in private.


#27

found something interesting

When your hero try to move or do an attack against an object like an enemy or an item it will always try to acess the attribute ID of the object. enemy.id item.id
the thing is when the enemy value is null it can’t acess to the attribute and when we try to
compare what is null well you are still trying to acess the attribute of the class enemy.

I tried to redefine what is enemy and have the root value checked if it is None, and I never had the bug that the enemy is already dead since.

class enemy:
    def __init__(self, enemy):
        if enemy != None:
            self.health = enemy.health
            self.id = enemy.id
        else: pass
    def attack(self):
        if self.health > 0:
            hero.attack(self.id)

target = hero.findNearestEnemy()
while True:
    monster = enemy(target)
    monster.attack()
    if monster.health <= 0:
        del monster
        target = hero.findNearestEnemy()

The only thing is you would have to redefine an object enemy for each actions you want to do so it would check if it still exists. I can’t think if something else for now