Possible to get a range of sensors?

I’m trying to find an efficient way to tell if any of a large range of sensors are positive. I can’t seem to find the correct python syntax for this though. It would be something like:

SensorArray = Controller.getSensors()[0]-[57]

if SensorArray.isPositive():

code...

Is this even possible? I’m just trying to find a better way to tell if any of 58 different sensors are positive without writing one huge if statement.

how about:
SensorArray = Controller.getSensors()[:57]

but why do you have so many sensors anyway?

This method doesn’t seem to work, it gives the error:

Attribute Error: ‘list’ object has no attribute ‘isPositive’

(the () brackets were definately present in the statement)

I have so many sensors since I need to determine whether the cursor is over any one of a number of objects in the scene, or none of them. So each needs a mouse over sensor attached to a central script.

There’s probably a simpler solution, but it’s just not coming to me at the moment :-?

Attribute Error: ‘list’ object has no attribute ‘isPositive’

How are you checking if the sensors are positive? I assume you’d need something like…


for sensor in SensorArray:
    if sensor.isPositive():
        do something...

cheers
Piran

Could you provide example code of this method? Would I need to loop through each element of the array to check whether it’s positive?

I simply used if SensorArray.isPositive(), but I guess that doesn’t work :slight_smile: I haven’t played around much with arrays in Python so I’m not sure how this should be set up.

why do you have so many sensors anyway? it probably would be possible to have very few and let python do the hard work

yeah, you’d need to loop over them


# I assume sensorarray has already been set

allPositive = 1
for sense in sensorarray:
   if not sense.isPositive():
      allPositive = 0
      break

if allPositive:
   # do stuff
   print "I eat wet socks

… or so I’d imagine

I would figure things like plants and decorations should just essentially one object. Using one sensor. Then objects that needed to be there for keys and other items could be placed seperatly. Although I dont know much about python (I know some about VB and I wish they would impliment VB script :wink: ) But cant you make an array of the other sensors? Cant each plant be esssentially plant(1).location and plant(2).location somehow?

##################
## Room 1 and 6 ##
##################

if g.getCurrentScene().getName() == "Room1" or g.getCurrentScene().getName() == "Room6":

	mOver1 = c.getSensors()[0]
	mOver2 = c.getSensors()[11]
	mOver3 = c.getSensors()[2]
	mOver4 = c.getSensors()[3]
	mOver5 = c.getSensors()[4]
	mOver6 = c.getSensors()[5]
	mOver7 = c.getSensors()[6]
	mOver8 = c.getSensors()[7]
	mOver9 = c.getSensors()[8]
	mOver10 = c.getSensors()[9]
	mOver11 = c.getSensors()[10]


	if not g.EquipedItem == " ":

		g.MouseOver = 2


	elif mOver1.isPositive() or mOver2.isPositive() or mOver3.isPositive() or mOver4.isPositive() or mOver5.isPositive() or mOver6.isPositive() or mOver7.isPositive() or mOver8.isPositive() or mOver9.isPositive() or mOver10.isPositive() or mOver11.isPositive():

		g.MouseOver = 1

	else:

		g.MouseOver = 0

Here’s a snippet of my code. It changes the cursor image dependant on the g.MouseOver variable. This is the only error-proof way I’ve found to do something like this.

Note one sensor is missing since that’s the pulsing always sensor.

I’ll experiment with looping through an array to make it all a bit tidier.

Your idea is possible in theory pryjon, but it would take ages to set up and be very specific to that particular scene. Hence a lot of work for a whole game!

I’m trying to develop generic scripts, which can easily be inserted into other scenes with minimal modifications.

maybe this will work


if g.getCurrentScene().getName() == "Room1" or g.getCurrentScene().getName() == "Room6": 
  
   positive = False
   sensorchecklist = [0,11,2,3,4,5,6,7,8,9,10]
   for x in sensorchecklist:
      if c.getSensors()[x].isPositive():
         positive = True

   if not g.EquipedItem == " ": 

      g.MouseOver = 2 


   elif positive == True: 

      g.MouseOver = 1 

   else: 

      g.MouseOver = 0

you can exit the for loop if it returns positive as there s no need to continue testing once one is positive i,m not intirely sure but i think this is done with the break command eg


for x in sensorchecklist:
      if c.getSensors()[x].isPositive():
         positive = True         
         break

Thanks Siegel, that code works perfectly and my script is now much tidier! :slight_smile:

np Andy83,

glad it worked had to edit the post about five times to work out kinks