script problem

Hi i have this wieird problem with this script. Maybe someone can help me solved it.

Well a have a random number, the first time i run (o[inicio]) i put an object in some position, then when is not the first time i change the position in Y of the object without changing X and Z. So i wrote this script.

Y = random.uniform(-15, 38)
print(“originalY”,Y)

if not o[“inicio”]:

print("newY", Y + 38.620) 
mira1.position = [mira1.position[0], Y+38.620 , mira1.position[2] -4] 
print("afterPos",mira1.position)  

else:

o["inicio"] = False     
 
print("Ybefore",Y + 38.620)     
mira1.position = [-10.045, Y+38.625, -1.940] 
print("beforePos",mira1.position)

the output, its weird because i ran the script. First line the value of the random Y. Ybefore is Y+38.620. And finally i have a position. But heres one problem mira1.position = [-10.045, Y+38.625, -1.940] but in this case is [-9.9876134824752807, 2.0657463645935059, 2.0657463645935059] ok in X isnt too much difference. in Y is a very huge different the position in Y should be 74.439802493835373 but is 2.0657463645935059, and in Z is a little difference it should be -1.940 but is 2.0657463645935059.

(‘originalY’, 35.819802493835383)
(‘Ybefore’, 74.439802493835373)
(‘beforePos’, [-9.9876134824752807, 35.412167758727961, 2.0657463645935059])
(‘originalY’, 1.5803624490442125)
(‘newY’, 40.200362449044206)
(‘afterPos’, [-9.9302269649505615, 1.1677277139367845, 2.0714927291870118])

someone know why it happen?

Please make sure to use the

 tags when posting python snippets, and don't forget to properly indent everything.

Now, as for your problem: I don't know exactly what's causing the discrepancies in your position values. You could be inadvertently changing the value of Y somewhere else, or maybe, if your object is dynamic, the physics engine is making its own moves overriding your position instructions.

A simple .blend that shows this problem in action would probably help.

In either case, I do know for a fact that this simple program works perfectly (assuming the object is static):


import GameLogic as GL
import random
cont = GL.getCurrentController()
own = cont.owner

rand = random.uniform(-5, 6)

if not “init” in own:
# Set the initial position of the object
own.position = [0, rand, 0]
own[“init”] = 1 # Initialization complete
else:
# Change Y only:
own.position = [own.position[0], rand, own.position[2]]

If you just use that, there should be no trouble.

PS: In python 2.6 "print" is still a statement, not a function. So, whenever you do <i>print("some_list", list)</i>, you're actually printing a tuple. (that's why your output was wrapped in parentheses). 

You can just do: <i>print "some_list", list</i>

Here is my .blend file so you can check it. I still dont know where is the problem.

Attachments

prueba1.blend (141 KB)

Un-parent mira1 from the camera, then try again; the values should be exact.

I need that mira1 is parent with the empty. There is a way to set a position to mira1 without it changes.