Help with new python api


range = owner.getDistanceTo(object_hit)
if range > 60:

have you any idea why this isn’t working?
i think i need to define both 60 and range as float values, but the new api is wierd.
Could you have a go? :slight_smile:
(blender 2.55)

CHANGED QUESTION, FIRST WAS A STUPID MISTAKE, BUT THANKS A LOT JOEMAN
*i’ve noticed you helping quite a lot!

Which if statement is failing? That information can help you to debug the problem. Also, you should just be able to say:

bge.logic.named = controller.owner,

or bge.logic.named = controller.owner.name

and then use sce.objects[bge.logic.named] to retrieve the object later.

EDIT: In your code, there’s no ) after str(owner[‘named’].

thanks, i fixed it;
it was the fact it used the name of the owner for a variable. the owner name was wrong XD
but i have learnt some stuff, btw…


range = owner.getDistanceTo(object_hit)
if range > 60:

have you any idea why this isn’t working?
i think i need to define both 60 and range as float values, but the new api is wierd.
Could you have a go? :slight_smile:
(blender 2.55)

If you have such problems please post the complete python source (at least until the line number of the stack trace) plus the error message. Otherwise you can only get a wild guess.

“…why this isn’t working” is not a problem description, nor it does tell what you want to achieve.

Is object_hit a KX_GameObject?

You are missing a “:” after “if …” (plus some more statements after that)

thanks monster, i corrected my error, yes, it is a KX_GameObject, taken from a ray sensor.
i need to compare the value of that to 60. They both should be FLOAT.
any ideas?

try this:

hit.py


def showDist(cont):
    own = cont.owner
    hitSensor = cont.sensors[0]
    if not hitSensor.hitObject:
        return
    
    range = own.getDistanceTo(hitSensor.hitObject)
    print (range)
    
    if range > 10:
        print ("large distance")

use module controller with hit.showDist connected to a mouse-over-any sensor (or ray sensor)

edit: I forgot … apply it to any object you want to measure from (e.g. camera)