Activating Sensors

I know that you can use pulse triggering to enable sensors to continuously run. Is there a way using python to trigger a sensor so that it does not run unnecessarily? I have a ray sensor and I need to use the hitPosition, but only when my character is falling.

You could do something like this:



import GameLogic as gl

cont = gl.getCurrentController()
own = cont.owner

#if moving in the -Z direction
isFalling = own.getLinearVelocity()[0] < 0

sensor = cont.sensors['sensorName']
if isFalling:
     #use hitPosition
     
    #activate sensor
    cont.activate(sensor)

Using controller.activate(actuator) does not work with sensors. Thanks though…

There is a very versatile ray casting function that can be accessed using python alone, which is documented here. If you’re only using the ray sensor so you can read the hit position, it’d probably be easier to use it rather than a sensor. Put it in a script that is run every logic tic, and inside an ‘if’ statement that checks to see if the character is falling.

Is that what you’re looking for?

the same thing can be done with the Ray sensor to get the hitPosition every logic tic. I was trying to find out if a sensor can be activated through python without it having to be run every logic tic, just when I need it.

Thanks

Wow, you respond fast :smiley:

I’m not aware of any way to flat-out disable a sensor, although I’m not a super-expert on the game engine. I guess deleting the object with the sensor on it will stop the sensor from running, but you can’t really do that if it’s on a character.

It is probably not possible, but thanks for your time.

I’m trying to find out how to do this exact thing now. In my limited understanding, I find it sucks that you can’t activate and deactivate sensors through python.

What I want is for the Ray sensor to shoot a ray when I press a key, return if an object with a certain property was detected, then not shoot any more rays.:slight_smile:

edit:
SOLUTION:
The best option I’ve found to enable and disable sensors is-
sensor.usePosPulseMode=1

sensor.useNegPulseMode=1
( or “=0” to disable neg or pos pulse mode)

So you can leave the Sensor pulse mode buttons set to off, then turn on pulse mode when you need it in python, and turn it back off right after- so it just does 1 pulse! Does what I wanted so far-just letting out one pulse when I want, rather than running forever, and making the whole script run forever too!
BGE Sensors API page

use states!

Sensors that are not connected to an active state are not evaluated. Wich means the ray sensor does not cost anything until the controller switches to the falling state.

It would be better for your logic anyway to have a falling state, walking state etc.

Just for information:
Switching the pulse mode off will not make the ray not be processed. It just makes the sensor not trigger the controller when the sensor’s state isn’t changed.
It still must be evaluated to find out its current state.

I hope it helps