Setting an object position via Python

Is it possible to set an object position via Python. I would have predefined X and Y coordinates.
I know I could use IPOs, but want to give Python I try.

There are some scripts for moving objects in my Snooker and Billiards games. You could maybe change one of those to suit your needs.


use the setPosition(posX, posY, posZ)
And to get the position of an object use getPosition()

You know some python alredy? then you will know the rest…
If not here goes…

import GameLogic
controller = GameLogic.getCurrentController() # This gets the Controller
owner = controller.getOwner() # This gets the Owner

From this you can set position and get the position of the owner

This Gets the position of the owner

owner_position = owner.getPosition()

And this sets the position

owner.setPosition(positionX, positionY, positionZ)

This mey help you :slight_smile:

NOR.J, setPosition() expects a list.

owner.setPosition([positionX, positionY, positionZ])

Thanks a lot, guys. No don´t know much (any…) Python, so the script above really helped me (with all the details given).
How could I do such a check:

If Property = 1 then owner.setPosition([positionX, positionY, positionZ])
If Property = 2 then owner.setPosition([positionX, positionY, positionZ])
If Property = 3 then owner.setPosition([positionX, positionY, positionZ])

Property = 0

And what are the keywords for rotation?
What documentation could tell me these sort of things?

oo yeah i forgott that…

oo yeah i forgott that…[/quote]

NP NOR.J. The only reason I remeber it is because I forgot it once, and it took me several hours to find what was wrong with my script :stuck_out_tongue:

A few more questions…
How can I get the position and set the new position to OldX, OldX, and only a new Z pos? How can I get just one item of the x,y,x list to a variable?
How can I specify a object name for the position to be aplyied to (other than the owner)? Properties?

obj.getPosition() retuns a 3 element list containing the position

everything beyond that is trivial

you can’t specify the object you want the position to be applied to by name, you have to get ahold of it somehow. By acquiring it through logicbricks.getOwner(), or appending it to gamelogic by a script which can get it through one of its logicbricks.

Sorry, but that´s not trivial to me…

I believe it’s:

owner.setPosition()[1] for the X value
owner.setPosition()[2] for the Y value
owner.setPosition()[3] for the Z value

nope, unless there is some new python syntax I must learn but all that will do is try to index the list that owner.setPosition() returns [it doesn’t return a list so you would get an error]

so, I’m a pretty insensitive person so I was going to respond to arangel that it IS a trivial problem and since he considered it not one he should learn how to program


okay then, if you only want to change one component of the location you would set the other components to what they were before

for example [this is untested code btw]

position = obj.getPosition()
obj.setPosition([position[0], position[1], 0.0])

to set only the z position of the object to 0.0

Thanks a lot, z3r0 d.
I´ll try to dive some more into Python. Just got my feet wet right now. :wink: