Get Parent Sensor with Python?

Hi :wink:

How I can know the state of a ray sensor if it’s attached to the parent object?

Cube1 (with a ray sensor) is parented to Cube2 (here is the python script)

What script is needed to get the sensor of Cube1 from Cube2?

Thanks in advance.

P.D: I can’t use the object name (Cube1) directly, because this script must be used in several objects with another names.

I can’t think of any functions to do that. What I would do would be trip a property when sensor was positive:

if ray.isPositive():
   owner.rayOn = 1
  owner.rayOn = 0

You can also send a message to the other cube when the sensor isPositive() and another message when it isn’t. You could connect the ray to a copy property actuator I think. I’m not too up on those. There is a reference here.

Try the following ways:

Idea 1:

  • Select both Cube --> you should get the logic brick of both Cube in your F4 panel
  • Wire the sensor of Cube 1 to the Cube 2 actuator --> the sensor should be reachable from your script with a usual getSensor () callIdea 2:
  • Add a python controller to Cube1 with a script looking like:
cont = GameLogic.getCurrentController()
GameLogic.sensor_state = cont.getSensor("the_ray_sensor_name")

  • in the Cube 2 script, use the sensor_state variable as if it was your sensor:
if hasattr(GameLogic,'sensor_state') == True:
    print "the sensor state is" , GameLogic.sensor_state
    print "Sensor state not yet initialized ... I'll try later on .."

If you enjoyed this reply:
visit this thread and post comment … :wink:

See ya …

Yeah, that would be easier. I forgot about that.

The IDEA 2 is what I was looking for.

Thanks guys! :wink: