can move object on scene but can't leave it

Hi, i want to move an object in a walkthorough, the user moves the camera with the keyboard and when he clicks an object he “catches” it and the object moves parented with the camera, i used an Empty to do this.
It works but I can’t leave the object where I want, I need to leave the object with a click so I have to sensors:
a Left Click sensor = senso
a Mouse Over sensor =senso1
2 properties: moved and click, to set that the user clicked the object and moved it

linked to this script:

from Rasterizer import showMouse
import GameLogic
cont = GameLogic.getCurrentController()
own = cont.getOwner()
showMouse(1)
scene = GameLogic.getCurrentScene()
senso = cont.getSensor(“senso”)
senso1 = cont.getSensor(“senso1”)

if senso.isPositive() and senso1.isPositive():
own.click = 1

object = scene.getObjectList()
for o in object:
if (o.Name == “OBEmpty”) and (own.click==1)
pos = o.getPosition()
own.SetPosition(pos)
own.moved = 1

if (own.moved ==1):
if (senso.isPositive()) and (senso1.isPostive()):
own.setPosition([1.0, 2.0, 3.0])

When I debug it i see that it never leaves the object in position (1,2,3) because senso.isPositive() never gets the value = 1 it’s always 0, no matter how many times i click the object, instead senso1 does get the 1 value.
Why?
and how could i make it work??

Well, it sounds like your mouseOver sensor isn’t triggering. If it was me, I would test that first by itself. A script that just tested the mouseover sensor on the object, if it was positive print “yes”. If it’s not working, you have to find out why and make changes or find an alternative.

I’m a newby: can’t find an alternative by myself and don’t have any idea of why the Left Mouse Button sensor isn’t working in the second click. The mouse Over is working, i’ve already debugged it, which surprises me more because one could expect to have more problems with the Mouse Over than the Left Mouse button, but i don’t know…

The Left Mouse Button sensor works when i catch the object but it’s not working when i want to leave the object, can’t figure why and can’t find an alternative by myself

I might could help if I could see the blend file. Can you post it?

No, unfortunately i can’t.
i copied all the script code by hand.

I think that the problem is that the object i moved is still joined to the Empty, i don’t know how to break this link between them, maybe deleting the Empty, i’m trying to do this with the Edit Object Actuator but i think i’ll need another script for doing this deletion of the Empty and don’t know how to deal with 2 scripts. I don’t know what to do. :frowning:

But there’s nothing else in the .blend, just the sensors and the script

Well, you should never post any amount of code like that without using the code tags. All the indention is lost and Python is unreadable without indention.
Even if I did look at it, I don’t know how you have your sensors set up. You’d have to post what type they are and they’re initial settings.

You are right, it loses indentation.
Now I added a second Left Button Click sensor and now it reaches the last if sentence but still not working, this are the sensors:

a Left Button Click sensor = “senso”
a Mouse Over sensor =“senso1”
another Left Button Click sensor = “sensor1”

2 properties: moved and click, to set that the user clicked the object and moved it, inicialized = 0
I’m using “senso” for catching the object and “sensor1” for droping it,
linked to this script:

from Rasterizer import showMouse
import GameLogic
cont = GameLogic.getCurrentController()
own = cont.getOwner()
showMouse(1)
scene = GameLogic.getCurrentScene()
senso = cont.getSensor(“senso”)
sensor1 = cont.getSensor(“sensor1”)

if senso.isPositive() and senso1.isPositive():
(tab)own.click = 1

object = scene.getObjectList()
for o in object:
(tab)if (o.Name == “OBEmpty”) and (own.click==1)
(tab tab)pos = o.getPosition()
(tab tab)own.SetPosition(pos)
(tab tab)own.moved = 1

if (own.moved ==1)and(senso.isPositive()) and (sensor1.isPositive()):
(tab)own.setPosition([1.0, 2.0, 3.0])

I debuged it printing messages and now it reaches the last sentence but something doesn’t let position the object, it kind of places the object in position (1.0, 2.0, 3.0) but it immediately returns the object to the position parented with the camera (the position of the Empty). It seems that i need something to “break the link” between the object and the Empty but don’t know how

Well, you need to post your full code for the script with the code tags if you want help. Above the box that you fill in when you are making a post are some buttons. When it comes time to post your code, press the code button first and it will make a tag. Then paste in your code. Then press the code button a second time.

Press Code button then paste then press Code button:

from Rasterizer import showMouse
import GameLogic
cont = GameLogic.getCurrentController()
own = cont.getOwner()
showMouse(1)
scene = GameLogic.getCurrentScene()
senso = cont.getSensor("senso")
senso1 = cont.getSensor("senso1")
sensor1 = cont.getSensor("sensor1")



if senso.isPositive() and senso1.isPositive():
	own.cli = 1

object = scene.getObjectList()
	
for o in object:
	if (o.name == "OBEmpty1") and (own.cli==1):
		
		pos = o.getPosition()
		ori = o.getOrientation()own.setPosition(pos)
		own.movi = 1
if (own.movi) and (senso1.isPositive()) and (sensor1.isPositive()):
	own.setPosition([1.5,2.28,0.932])
	

ori = o.getOrientation()own.setPosition(pos)

What are you trying to do with this line? If, for nothing else than clarity, you put multiple commands on different lines, unless your actually wanting what that combined statement is.

Secondly, think about your logic there. If both sensors are positive, your setting the owner position twice, or at least it looks like it to me. Actually, it’s hard for me to tell what’s going on there. Naming sensor senso and senso1, isn’t good practice. Name them what they do and your code will be more comprehensible. MouseOverSensor = blah blah, MouseLeftClickSensor = blah. It’s long, but you’ll know at a glance what in the world your trying to do. Then use copy/paste when you reuse them so you don’t mistype.

this should work better but still pretty pointless to me


from Rasterizer import showMouse 
import GameLogic 
cont = GameLogic.getCurrentController() 
own = cont.getOwner() 
showMouse(1) 
scene = GameLogic.getCurrentScene() 
senso = cont.getSensor("senso") 
senso1 = cont.getSensor("senso1") 
sensor1 = cont.getSensor("sensor1") 



if senso.isPositive() and senso1.isPositive(): 
   own.cli = 1 

object = scene.getObjectList() 
    
for o in object: 
   if (o.name == "OBEmpty1") and (own.cli==1) and (sensor1.isPositive()): 
      own.setPosition([1.5,2.28,0.932]) 

Still doesn’t really make sense cause you are saying if you click and a object exists in scene(witch it always will if it’s there and not ended) set the position on the object running the script.

You need to look closely at what you are trying to accomplish

What i want to do is this:
1.with left click button catch an object (to do this i use sensor “senso” and “senso1”
senso=left click button
senso1=mouse over
2. parent the object with the camera, to do this i use the Empty, that’s why i use the for sentence
So far it works OK: i can catch the object and carry it with me when i move in a walkthrough, so far the object moves with the camera
3. I want to let the object go with a Left button click, that’s what not working: when i click i want to leave the object at position (1.5, 2.28,0.932)
To do this i use the “sensor1” left click button sensor, but this one i think shouldn’t be necesary

I need to make some kind of SWITCH sentence to distinguish between this 3 cases, i’m trying to do it with if /else, but it’s not working

maybe this will work(this script is running on empty, with empty parented to camera, in mind)


from Rasterizer import showMouse 
import GameLogic 
cont = GameLogic.getCurrentController() 
own = cont.getOwner() 
showMouse(1)  
mouseover = cont.getSensor("senso") 
leftclick = cont.getSensor("senso1") 

try: 
   clicked 
except(AttributeError):
   clicked = 0
 
if mouseover.isPositive() and leftclick.isPositive() or (clicked == 1): 
   obj = mouseover.getHitObject() 
   obj.setPosition(own.getPosition())
   clicked = 1
   
if (clicked == 1) and leftclick.isPositive(): 
      own.setPosition([1.5,2.28,0.932])
      clicked = 0

Thanks, this is my second script, Siegel i don’t understand how does this script work:
the script runs on the empty so cont is the empty controller and senso and senso1 are the empty’s sensors, the first if sentence never executes because mouseover.isPositive() = 0 always.
i think that i don’t have to link this to the object i want to move, it serves for any object but how do i “contact” the object?

how about (i cant test these scripts at the moment since i do not have access to blender right now so they are more an idea than actual working code)


from Rasterizer import showMouse 
import GameLogic 
cont = GameLogic.getCurrentController() 
own = cont.getOwner() 
showMouse(1)  
mouseover = cont.getSensor("senso") 
leftclick = cont.getSensor("senso1") 

try: 
   clicked 
except(AttributeError): 
   clicked = 0 

obj = mouseover.getHitObject() 
if (obj.name=="ball") and leftclick.isPositive() or (clicked == 1): 
   obj.setPosition(own.getPosition()) 
   clicked = 1 
    
if (clicked == 1) and leftclick.isPositive(): 
      own.setPosition([1.5,2.28,0.932]) 
      clicked = 0 

ok, thanks, i started with python a week ago so i’m a disaster

Been looking for the getHitObject and i need a Collision sensor or a touch sensor for it to work, don’t know how to use it here, because my mouseover sensor is a Mouse sensor which is on the Empty.

I was running the script i wrote on the object i want to catch (and move and leave) but there the problem is to differenciate between the first click to catch the object and the second click to leave it, i’m trying to do some kind of case/switch structure but don’t know how to differenciate between the click to catch and the click to drop the object, i’m trying to use the clicked property but isn’t working

i will play around with it as soon as i get a chance

I think that i don’t need to say this, but the script i wrote is also linked to an Always Sensor, which i don’t use in the script, but it’s firing pulses all the time and this is why i can’t differenciate between the first click (to catch the object) and the second click (to drop it)

but it’s firing pulses all the time and this is why i can’t differenciate between the first click (to catch the object) and the second click (to drop it)

You usually use properties for that purpose. You can make these local or global, depending. So, on the first click, you might have a global property like GameLogic.isPickedUp = 0.


 if GameLogic.isPickedUp == 1:
    # drop item code
    GameLogic.isPickedUp = 0
 else:
    GameLogic.isPickedUp = 1 
    #pick up item code

It’s a good idea to keep all your global properties together and initialize them when the game starts up. An easy way to do that is to have an always sensor with both the positive and negative pulses turned off. This will cause it to fire once at start up. You connect that to a python controller called initialize or init and just put you globals in there after you import GameLogic. In this case it would be the one line:

GameLogic.isPickedUp = 0

fireside, i can’t try it now but what you say is that i need a python controller to initialize the isPickedUp =0 when i leftclick the object and the same controller would set isPickedUp=1 with another leftclick in the object when i want to drop the object? (this script running in the object i want to move). This script is just for setting the isPickedUp property and will be linked with a mouse over and a left click button sensor.

And another script in the object i want to move which would be something like this:
if isPickedUp=1
code to move
else: setPosition ([1,1,1])