Why do debug tell me I use a null value


def GetCloser(maTarget):
    if maTarget and hero.distanceTo(maTarget)>10:
        midX = (hero.pos.x + maTarget.pos.x)/2
        midY = (hero.pos.y + maTarget.pos.y)/2
        hero.moveXY(midX,midY)
        GetCloser(maTarget)
    elif maTarget :
        hero.moveXY(maTarget.pos.x,maTarget.pos.y)

so debug tells me that hero.moveXY(maTarget.pos.x,maTarget.pos.y) uses null parameter instead of x=integer and y=integer. I thought that the elif would be false so that part of the code would not be executed.

2 Likes

Do you have boots with the moveXY? It could be an equipment problem

1 Like

yep i have those boots. and the code runs for a while.

1 Like

Then what happens? :thought_balloon:

1 Like

As far as I understand coding : if maTarget.pos.x is null, it’s because maTarget is also null. So I do not get why anything after the elif would ever be executed. So I should be wrong somewhere and therefore ask if someone knows about it.

1 Like

What is ma Target. You have to define what ma Target is

1 Like

It’s called from this part of code and only this

cible = hero.findNearestEnemy()
if cible:
    GetCloser(cible)

so here it is an enemy, but it could also be an item. btw this is python

1 Like

So you are trying to go to the enemy.pos if so then put cible instead of ma Target

1 Like

when you call : # call to the function/method)
GetCloser(anyEnemy)
GetCloser(anyItem)
maTarget = anyEnemy
maTarget = anyItem

or there wouldn’t be any benefits to define functions/methods

1 Like

Which level are you doing?

1 Like

stonehold siege? not sure about the name in English (last one in the forest). but the question is not especially about that level.

1 Like

oh ok but you don’t really need that you can just command all of your troops to defend you

1 Like

and attack the enemies then you can easily win with any hero

1 Like

Is this question about multyplayer levels?

1 Like

not especially. but could be used to optimze your movements on the map and avoid moving to dead enemies.
(only if the code was running as intended :’( )

1 Like

Sometimes maTarget exists when you call GetCloser(maTarget) function, but dies or gets invisible on the 2d or 3d line of function, so hero stucks. Depends on loops and other code, of course. But it made me agrrhh! many times)

2 Likes

I suppose you are trying to do recursion and the general idea is

def GetCloser(maTarget):
    if maTarget and maTarget > 3:
        mid = maTarget/2
        return GetCloser(mid)
    else :
        return True

GetCloser(37) 

I’m also eager to know how this algorithm can be implemented with moveXY and move

1 Like

yup. the idea is :

  • maTarget is any object with position so pos.x and pos.y. lets call it enemy
    if the distance between hero and enemy is too much (10 here) and enemy still exist , I do half the travel. then call GetCloser() again
    if the distance is ok (<10) and enemi still exist go to enemi.
1 Like

I understand your intentions and already tested the code. If this is some kind of recursion? Did you click on the link?

1 Like

yup, I clicked. and yes it is recursion as far as a function call itself (if I remember well what recursion is :wink: )

1 Like