Hi everybody!
Some time ago I started a small project using Blender GE, but being a complete newbie, I had a problem I couldn’t overcome and now I’m completely stuck.
I set up an environment in a .blend file- a really simple one, with just a floor, walls and a table where things must be placed. Everytime I play this, a python script is run, thanks to which 5 objects are automatically appended from separate .blend files and placed on the table.
What I want to do is give the player the ability to select one of the objects by clicking on it… And of course my .blend should be able to somehow tell me which object was selected.
Here’s how I thought I’d handle it (as I told you, it doesn’t work): using logic bricks, in the .blend file of every object I’ve added a left click sensor linked to a python controller in which the name of the script to be run on click event was left blank.
In the environment .blend file (which is the main file where the object are appended) I wrote the small script meant to be run on click event, “printobj.py”, whose main goal is to tell me what object was clicked.
Then, using the main python script (called “test.py” and contained in the environment .blend file) I appended each of the objects and then assigned “printobj.py” as target of each controller.
The code of the “printobj.py” file is the following:
import bpy
import bge
cont = bge.logic.getCurrentController()
for sensor in cont.sensors:
print("sensor " + sensor.name)
if sensor.positive:
print("true " + str(sensor.name))
else:
print("false " + str(sensor.name))
The idea is that only one of the mouse click sensors associated to the 5 objects should be positive when I click on that specific object, while all the others should be negative. So, under the hypotesis I clicked on the object “Pan”, I’d expect an output like:
sensor MouseToaster
sensor MousePan
true MousePan
sensor MousePizza
sensor MouseMoka
sensor MouseSpatula
What I have instead is something like this:
sensor MouseToaster
true MouseToaster
sensor MousePan
true MousePan
sensor MousePizza
true MousePizza
sensor MouseMoka
true MouseMoka
sensor MouseSpatula
true MouseSpatula
sensor MouseToaster
false MouseToaster
sensor MousePan
false MousePan
sensor MousePizza
false MousePizza
sensor MouseMoka
false MouseMoka
sensor MouseSpatula
false MouseSpatula
So not only the script is apparently executed twice (while I only click once, on just one of the 5 available objects), but the first time ALL the sensors are positive, while only one should be.
I really don’t understand what’s wrong… Can you give me an idea?
Thanks in advance!
Lovell
PS: I hope what I wrote is clear… If not, just tell me, and I’ll try to explain it better!