two sensors

How do you activate two sensor connected a and controller,connected to a actuator?

import bge
from bge import logic
cont = logic.getCurrentController() 
obj = cont.owner
sens = cont.sensors["Mole"]
sens = cont.sensors["Pop"]
sens.propName = "Knife" 
sens.propName = "Junp" 

actuator = cont.actuators["Enemy"]
if sens.positive == True:
        cont.activate('Enemy')
else:
        cont.deactivate('Enemy')
        
        
        

I don’t really get what you want to achieve but there is a mistake in your code.
You overwrite the “Mole” sensor with the “Pop” sensor

How do i correct the overwrite if there is one now?I hunted for code on the blender forum and found a piece of this code.It still triggers with one property.

import bge
cont = bge.logic.getCurrentController()

ko = cont.sensors["Mole"]
ut = cont.sensors["Pop"]
actuator = cont.actuators["Enemy"]
if ko.positive and ut.positive == True:
        cont.activate('Enemy')
else:
        cont.deactivate('Enemy')
        
        
        

Thats a good question actually. I don’t know of a way to get sensors and actuators that are not connected to your python code, but I also can’t think of a reason would would need to do that…

Just delete your “and” controller and replace it with a python one, then your script should work fine (if programmed correctly)

I don’t understand the question. Here’s some advice based on my assumptions…

If you’re asking how to get the sensors from another object, it’s very simple.


obj = logic.bge.getCurrentScene().objects['Object Name that has the sensor']

sensor = obj.sensors['name of sensor in the object']

I can’t test this right now, but that works for me.

If you’re asking about your errors…
For the parameters in cont.activate(actuator), you don’t put in a string. You put in the variable name for the actuator variable you created.

So not:
cont.activate(‘Enemy’)
but, in your case,
cont.activate(actuator)

You should also learn how to read errors (look in the blender console). When you’re inexperienced with programming it’s difficult to tell what you did wrong syntax/API wise without looking at the error message.

If you’re asking how to activate an actuator using two sensors at the same time, 3D solar system builder, answered that I think.

Maybe if you describe what you really want to achieve we can help better.

Currently this code makes no much sense.

My two sensors are connected two a python controller and the python controller is connected to a actuator.

You are correct i am asking how to activate an actuator using two sensors at the same time.Where did you explain that?

I used a script template named gamelogic simple to help me make it and used the blender forum search function for more help but could not figure it out.Because it was the easiest way to do it.

Do what? I still do not know what it is :wink:

If I understand correctly what you’re saying, a simple AND statement in conjunction with the sensor.positive checks is all you need.

from bge import logic

cont = logic.getCurrentController() 
obj = cont.owner

moleSens = cont.sensors["Mole"]
popSens= cont.sensors["Pop"]
moleSens.propName = "Knife" 
popSens.propName = "Junp" 

actuator = cont.actuators["Enemy"]
if moleSens.positive == True and popSens.positive == True:
        cont.activate(actuator)
else:
        cont.deactivate(actuator)

The script is supposed to trigger the actuator only when both sensors detects both properties of Knife and Junp.But it doesn’t.The actuator is triggered by the detection of one property.

That’s because you were only checking one sensor in your original script. This new script checks both.

The actuator is triggered by either property or both at the same time.What could be causing this?I am using blender2.65. I did try your script.

That’s only possible if both sensors are True in the same frame or if you have them hooked up to some other controllers besides the Python controller. I suggest you do some testing to see if the sensors evaluate to True only when you expect them to.

Hmm so you need two steps:
A) setup the sensors to listen to the right properties
B) check if the sensors go positive, if so activate the actuator

you can do both with GUI, so there is really no need to fiddle with python.

Property: Mole
Property: Pop

Property Sensor Property: Mole equals Knife –
Property Sensor Property: Pop equals Junp -----> AND --> Actuator

As code:


def setupSensors(cont):
   '''Execute it before checking the sensors'''
   mole = cont.sensors["Mole"]
   pop = cont.sensors["Pop"]
   mole.propName = "Knife"
   pop.propName = "Junp"

def activateWhenAllPositive(cont):
   '''Executed after setup'''
   mole = cont.sensors["Mole"]
   pop = cont.sensors["Pop"]
   
   if mole.positive and pop.positive:
      cont.activate("Enemy")
   else:
      cont.deactivate("Enemy")

This code is better written with logic bricks.

BTW. This logic does not include anything that changes the properties to “Knife” or “Junp”

Logic bricks are buggy and python seems to have bugs in it.So you are saying your code will not detect properties Junp and Knife.I thought you had to put the logic bricks on true pulse.

But i want to use two near sensor to detect two properties that use a python script to activate a steering actuator.

But near sensors do not detect properties. They detect game objects. A near sensor can indeed filter by one property. This still allows logic bricks.
According to your above questions:
Do you want to find objects with a property?
or a specific value in the property?
and/or the presence of both properties with the given values?

But near sensors are “heavy” sensors. So they should used as less as possible. In your situation I recommend a Python controller as it will be more efficient than two near sensors.

What you can do is this:

You sense with ONE near sensor without a filter.
You let a Python controller filter the sensed objects.
If the specified objects are detected you can activate the connected actuators.

Try this code:


def activateOnFilteredHitObject(cont):
   hitObjects = getHitObjects(cont)

   moleObjects = objectsWithProperty(hitObjects, "Mole")
   junpObjects = objectsWithProperty(hitObjects, "Junp")
   
   if moleObjects and junpObjects:
     activateAllActuators(cont)

def getHitObjects(cont):
   hitObjects = []
   for sensor in cont.sensors:
      try:
         sensorHitObjects = sensor.hitObjects
      except AttributeError:
         continue
      hitObjects.extend(sensorHitObjects)
   return hitObjects

def objectsWithProperty(gameObjects, propertyName):
   return [obj for obj in gameObjects if propertyName in obj]

def activateAllActuators(cont):
   for actuator in cont.actuators:
      cont.activate(actuator)

This is untested. I hope it matches what you want to get.

I want to find objects with two properties.A simple script not too long.So i can understand how to program the blender game engine better.