2
$\begingroup$

I'm trying to build a simple user interface in Blender Game Engine. 2 buttons (launch and quit).

Each button got a "Mousse Over" and "Left Button" sensors connect to a one Python controller.

Here my script menu.py:

import bge

cont = bge.logic.getCurrentController()
own = cont.owner

OverLancer = cont.sensors['Over.Lancer']
ClickLancer = cont.sensors['Click.Lancer']
OverQuitter = cont.sensors['Over.Quitter']
ClickQuitter = cont.sensors['Click.Quitter']

if OverLancer.positive and ClickLancer.positive :
    print("lancer")

if OverQuitter.positive and ClickQuitter.positive :
    print("Quitter")

Nothing works as expected. I got the error "sensor not found" for all of them and I don't understand where I'm wrong. I saw as well something strange. My sensor names change like that .Blender add .001 sometime ...why?

And there is old deleted code line in the error message, again strange.

Python script error - object 'Bouton lancer', controller 'And.002':
Traceback (most recent call last):
  File "menu.py", line 6, in <module>
    print (cont.sensors)
KeyError: 'requested item "Over.Lancer" does not exist'

For example "print (cont.sensors)" is not present in menu.py !?!?

If anybody got an idea, it's will be great.

Thanks!

enter image description here

$\endgroup$

4 Answers 4

1
$\begingroup$

Each object has a self "scope" or "namespace", so the (object 1) controller can no access sensors from (object 2) controller even if they are using the same script, which is your case.

BUT you CAN CONNECT SENSORS from multiples to one (or more) CONTROLERS

Simple select all objects which sensors would be used and at the last select the object with the controller. to select multiple objects keep press Shit and Mouse Rigth Button on objects.

objects: Sphere has "Mouse_Sphere" sensor while Cube has "Mouse_Cube" and i want to use each sensor in one script and the controller is in Torus.

enter image description here

[enter image description here]1]

$\endgroup$
1
  • $\begingroup$ Thanks for your reply. I try this and i keep you in touch. $\endgroup$ Commented Sep 27, 2017 at 19:30
0
$\begingroup$

Starting at cont = bge.logic.getCurrentController() this line gives you the controller, that is the block that you set to use the script. cont.sensors is then a list of sensors connected to the controller.

Note that in your screenshot you have Over.Quitter and Click.Quitter connected to the script controller. The script however appears to be designed to work with both the quitter and lancer buttons, but only two of the four sensors is connected to it, so you get the "item does not exist" error.

While you can use a separate script for each controller, it is possible to make a generic setup. To make this work, each button needs two sensors, use the same sensor names for each button, so call them Click and Over then in the script you get the two sensors by name and to know which button is being clicked you use own.name.

import bge

cont = bge.logic.getCurrentController()
own = cont.owner

Over = cont.sensors['Over']
Click = cont.sensors['Click']

if Over.positive and Click.positive :
    print(own.name, 'clicked')
$\endgroup$
1
  • $\begingroup$ Thanks you. I think it's the simplest solution for me. I try and i keep you in touch. $\endgroup$ Commented Sep 27, 2017 at 19:36
0
$\begingroup$

Analysis

Be aware your code expects all four sensors to be present.

But your business (and setup) is to get just two of the sensors at the time.

You code complains about the missing sensors.

Solutions

There are several different solutions. Here are some of them.

Simple Isolation

I suggest to focus on a single operation. This means you do not mix Lancer with Quitter. This is pretty simple as there are different objects with different logic already.

Button.quitter executes code that deals with quitting while button.lancer deals with "lancer"-ing.

The button logic is pretty much the same. The difference is the triggered operation.

If you do not mix in other operations you do not need to care the button names. You trigger the operation when all connected sensors evaluate positive (mouse over object + mouse button):

import bge

controller = bge.logic.getCurrentController()


if all(sensor.positive for sensor in controller.sensors):
    print("clicked") # trigger your operation here

Independent Triggers

I prefer to separate the button behavior from the operation even further. This is as all buttons behave the same (mouse over + click) Independent from the final operation. Additional it allows you to have other triggers for the same operations (e.g. via keyboard key).

The setup is a bit more complex and simpler at the same time:

Menu Button:

Mouse over sensor + Mouse clock sensor --> AND Controller--> Message actuator Subject: "quit"

Quitter: Message sensor Subject "quit" -> Python controller "quitting"

import bge

controller = bge.logic.getCurrentController()


if all(sensor.positive for sensor in controller.sensors):
    print("clicked") # trigger your operation here

(Basically the same code as above. The object just contains other sensors - the message sensor).

$\endgroup$
-1
$\begingroup$

Thanks, it's works !!! I used the Strapicarus method to solve my problem. I connected my sensors to a unique "select multiple objects keep press Shit and Mouse Rigth Button on objects."

here my script:

import bge

cont = bge.logic.getCurrentController()
own = cont.owner

OverLancer = cont.sensors['Over.Lancer']
ClickLancer = cont.sensors['Mouse_lancer']
OverQuitter = cont.sensors['Over.Quitter']
ClickQuitter = cont.sensors['Mouse_quitter']

if ( ClickLancer.positive and OverLancer.positive):
    print("ClickLancer")

if ( ClickQuitter.positive and OverQuitter.positive):
    print("ClickQuitter")

my logics: enter image description here

$\endgroup$
1
  • $\begingroup$ I give you the answer and you take the points... $\endgroup$ Commented Oct 10, 2017 at 1:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .