How can I call...

How can I call a sensor of an object in a python script different from which object’s logic it is placed in?

I think it should look something like this: (This one doesn’t work at all, and some part of it was only my mind-creation, just to show you what 'd like to get)

ray1 = scene.objects[“ray1_base”].getCurrentController().sensors[“ray_backwards1”]
ray2 = scene.objects[“ray2_base”].getCurrentController().sensors[“ray_backwards2”]
ray3 = scene.objects[“ray3_base”].getCurrentController().sensors[“ray_backwards3”]
ray4 = scene.objects[“ray4_base”].getCurrentController().sensors[“ray_backwards4”]

Would be very glad for some answers. :slight_smile:

A KX_GameObject has a “sensors” property (dubbed experimental) that lists the sensors attached to the corresponding blender object:

abcSensor = scene.objects[xyz].sensors[abc]

Why do you want to access the sensor of another object? The currently running object is not supposed to know the internals of another object. Better avoid such structures. They add confusion and have an high potential of errors.

Beside of that you can only trust the output of sensors that are connected to the current controller. The same with actuators … you can activate them by connected controllers only. Hint: you can connect logic bricks between different objects. But this looks ugly … it is the same missing beauty as accessing unrelated logic bricks with Python just more obvious.

If you look at your snippet you can discover the little word “current”. “current” indicates there is a context. There is only one “current” controller in context and that is the controller “currently” running. Knowing that let you easily guess other objects can’t have a “current” controller. You can access attributes of other objects as long as they exist. Please refer to the BGE API to see what is available ;).

Thanks a lot for the answers guys.

I am trying to set up something like a bounding box for the camera in a 3rd person view: I made 4 rays pointing backwards from one small cube each. When they detect an object in their range, the camera is moved to a location, counted from the place where one or more of the rays hit the object.

I think this to be lot better than the one-ray option, since that way I can easily look through walls…

Hey just to let you know, you can shoot rays from your camera directly! No need to go through parenting cubes and crap. It’s the system I use, and it works great

Aaand, that way you can disable somehow seeing through walls too? Because that’d be great. :slight_smile:

And another question: How can I call a property in python?

It’s simply


By keep it from seeing through walls do you mean moving the camera when it gets close to walls? If so there have been some examples of that, Mobious’ camera, although it only uses 2 rays, and the third person platformer example, both of which are in the resource section. I’m also pretty sure you could find more if you googled around

Thanks for the help again. :slight_smile:
I never tought it’d be this simple redbaron. :smiley:

Now it works just fine - at least didn’t find anything unwanted YET :slight_smile:

I didn’t want just copiing someone else’s for 2 rasons:

  • The ones I found mostly weren’t to my liking, since their cameras were rather following (going after) the player than constantly floating behind it and looking just the same direction where the character does.
  • And since I am learning this just now, so it can be a nice lesson once I figure it out. :smiley:

And thanks again all :slight_smile:

It’s awesome that you’re doing this yourself, but don’t be afraid to go and look at what other people have done and take inspiration! It’s the only way I’ve made any progress on my game