Why do debug tell me I use a null value


#1

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

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


#3

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


#4

Then what happens? :thought_balloon:


#5

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.


#6

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


#7

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


#8

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


#9

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


#10

Which level are you doing?


#11

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


#12

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


#13

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


#14

Is this question about multyplayer levels?


#15

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 :’( )


#16

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)


#17

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


#18

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.

#19

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


#20

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