Python controller multiple sensors and passing variables.

Quick question:

Is there an easy way to make a python controller with multiple sensor inputs treat the multiple inputs as an AND condition?

For example, have 2 sensors going to a python module controller. A mouse click and a mouse over. For some reason I was thinking it would treat the sensors like an AND, where both sensors would have to be positive for it to trigger, but that is not the case… is there an easy way to treat them like an and, or do I have to add python code to make that work…

2nd question, I posted on the python forum, but no luck:

Is it possible to pass variables directly to a module using a python controller, like so:

Module: Menu_Func.mouse(show)

or

Module: Menu_Func.muse(hide)

It doesn’t appear to work when I try this?

i not sure to understand what you want.
i have the suspect that you have some common issue with “trigger factor” :smiley:

a real easy solution is put an always sensor true , plus all sensor that you want .
not give the “control” to the sensor , by pass it , with always true .

of course for obj simple that run 2 line of code can worth keep instead some sensor that trigger instead (to avoid to run every time the script) without by-pass with always true

PS: i mean this:

Attachments


Hmm, I might not be understanding what you mean either… I’ll see if I can figure it out… to help clarify, I will post the following picture:


As you can see, I have 2 sensors running to my python module running: “Menu_Func.mousehide”

First question: EITHER of those two sensors “Mouse_Click” or “Mouse_On” will trigger the python module… I was wondering if there was an easy way to make sure it would take BOTH of those sensors triggered at one time (AND logic) to trigger the python module…

And secondly, I would love for that module to read “Menu_Func.mouse(hide)”, so I could write one function and extract the variable from the logic brick…

for your second question: U want to use the python module to show and hide the mouse? and thats not working? If so here is a little script:

from Rasterizer import showMouse

def yes():
    showMouse(1)
def no():
    showMouse(0)

What to do: put an Always (clean no true pulse needed) to a python controller and put it on module. Use “Script_Name.yes” or “Script_Name.no” to show or hide the mouse(without the “” ofcourse).

ok, after heavy test: D
I noticed that the python ever starts
if one of the sensors is positive ,not both, as I would expect(…)

I think you should assign a property, or change state

(therefore -> always true still the real solution :wink: )

for, the parenthesis I do not think is possible (but not sure)

  1. Yes, just check the sensors to see if they’re all positive:


from bge import logic

cont = logic.getCurrentController()
mouseover = cont.sensors['MouseOver']
mouseclick = cont.sensors['MouseClick']

if mouseover.positive and mouseclick.positive:
     print ("Both are positive")


A Python controller is triggered by whatever sensors are connected to it, as you have observed. You use Python to test those values and form your conditions to continue game logic or activate actuators.

  1. The Module mode is a bit different from usual Python syntax, as a function used in Module mode always gets passed the controller running the function, and you can’t customize the function, i.e.


def Main(cont):
     obj = cont.owner

# Instead of

def Main():
     cont = logic.getCurrentController()
     obj = cont.owner


If you want to, you can use a simple object property (in the actual Blender object, in the Game Logic window) to change how the function runs, i.e.



def mouse(cont):
     obj = cont.owner
     if obj['mouseon']:
          print ('mouse is on')
     else:
          print ('mouse is off')


Then, you can just alter the ‘mouseon’ property in your object.

Btw: u got those 2 controllers connected to python, why not just ad a few lines of code at the top? so u get this:

import bge
cont = bge.logic.getCurrentController()
#sensors
mc = cont.sensors["m_click"]
mo = cont.sensors["m_over"]
if mc.positive and mo.positive;
    #do this and that

this way it doesnt matter if python gets started becouse the scripts puts it on hold till both are positive // EDIT: SolarLune was just a bit faster.

Thanks guys, the python at the top with sensor checks should have been obvious, don’t know how I missed that, is an easy solution.

It is too bad about the python module mode, would be nice to pass variables to the function directly, I was just going to duplicate the code I needed for each module, but I think SolarLune has a good idea I didn’t think about with the property… seems like it would clean my code up a bit using that.

Have a look at the BGE Guide to Python Coding. It explains the things with the sensors and give s you same flexible examples how to deal with them.

No you can’t pass additional arguments to a module call. As cotax wrote you can have multiple entry points to your module (see gude above). If you want to transfer data you can use properties. But they differentiate objects only, not controllers.

I hope it helps