ANOTHER Python Problem

WOW I’m having lots of problems today. Here’s a wierd thing i noticed in Python. If you do this…

cont = GameLogic.getCurrentController()
ray = cont.getSensor("ray")
ray2 = cont.getSensor("ray2")

if ray.isPositive():
     object = ray.getHitObject()
 
if ray2.isPositive():
     newPos = ray2.getHitPosition()
     object.setPosition(newPos)

you get this…
AttributeError: type object ‘object’ has no attribute ‘setPosition’

I heard that there is a workaround for this, but I can’t find it anywhere. Does anyone know how to fix it?


cont = GameLogic.getCurrentController()
ray = cont.getSensor("ray")
ray2 = cont.getSensor("ray2")

object = None

if ray.isPositive():
     object = ray.getHitObject()
 
if ray2.isPositive():
     newPos = ray2.getHitPosition()
     if object: object.setPosition(newPos) #only move the object if it was defined before and its not None

note: please, make more descriptive thread titles, to make future thread searches easier ^ ^

My bad…

My example script is bad. Here is the actual script I wrote…

cont = GameLogic.getCurrentController()
own = cont.getOwner()
aimingRay = cont.getSensor("aimingRay")
laserRay = cont.getSensor("laserRay")
leftClick = cont.getSensor("leftClick")
rightClick = cont.getSensor("rightClick")
middleMouseButton = cont.getSensor("middleMouseButton")

if own.teleportMode == True:
     if aimingRay.isPositive() and leftClick.isPositive():
          object = aimingRay.getHitObject()
          own.teleport1Selected = True
          print "select1"
	
if own.teleportMode == True:
     if laserRay.isPositive() and rightClick.isPositive():
          teleportSpot = laserRay.getHitPosition()
          own.teleport2Selected = True
          print "select2"
	
if own.teleport1Selected == True:
     if own.teleport2Selected == True:
          if middleMouseButton.isPositive():
               object.setPosition(teleportSpot)

please, use a code tag, i cant see the identation there

sorry. I didn’t notice i used the wrong one lol

try this


cont = GameLogic.getCurrentController()
own = cont.getOwner()
aimingRay = cont.getSensor("aimingRay")
laserRay = cont.getSensor("laserRay")
leftClick = cont.getSensor("leftClick")
rightClick = cont.getSensor("rightClick")
middleMouseButton = cont.getSensor("middleMouseButton")

if own.teleportMode == True:
     if aimingRay.isPositive() and leftClick.isPositive():
          object = aimingRay.getHitObject()
          own.teleport1Selected = True
          print "select1"
     else:
          own.teleport1Selected = False

     if laserRay.isPositive() and rightClick.isPositive():
          teleportSpot = laserRay.getHitPosition()
          own.teleport2Selected = True
          print "select2"
     else:
          own.teleport2Selected = False
	
     if own.teleport1Selected and own.teleport2Selected:
          if middleMouseButton.isPositive():
               object.setPosition(teleportSpot)

can’t do that. I need teleport1Selected and teleport2Selected to stay true once they have been triggered. I can’t find out what’s wrong. I’ll just try rewriting the script

I found out the problem. It’s nothing wrong with the script. I can’t really explain it though. Thanks for trying to help though.