Python Code for Sensors

Once I getCurrentController can I find out which sensor sent to my python script? If so how?

Also, I am a bit concerned. If I use a timer to sequence events will that change on other computers depending on speeds?

cont = GameLogic.getCurrentController()
asensor = cont.getSensor("sense1")
bsensor = cont.getSensors()[3]

if asensor.isPositive():
   # asensor is true
   pass

if bsensor.isPositive():
   # bsensor [fourth sensor] is true
   pass

notice that I said that bsensor is the foruth sensor, and that I said it was true [not that it pulsed even, or even was the one that started the script]. This is about all the info you get, but by changing the pulse modes you can get the script to run when you like. [and by modifying the script, run how you like]

What is the [3] for at the end of getSensors?

just a number, it is an index into a list [getSenors() returns a list of the sensors connected to a controller]

an index of 3 is the 4th item in the list [because list indicies start at 0]

usually you would want to get a sensor by name, but perhaps you just want to get a sensor connected to the controller, regardless of the name. That would be cont.getSensors()[0] [the first sensor]

So they are an array? Excellent. Thank you! :smiley:

Using getSensors() isn’t always the best way to do it, since if the order of the logic bricks gets changed, the script won’t work right.

getSensor(“name”) is recommended. (It also makes the script easier to debug, since it’s more obvious which sensor it is.)