Hello,
I normally don’t have any issues with python, but I just stumbled upon something… strange.
Here’s the code I used:
def winSwap(ob1,ob2):
pos1 = objs[ob1].worldPosition
pos2 = objs[ob2].worldPosition
print(pos1,pos2)
objs[ob1].worldPosition = pos2
objs[ob2].worldPosition = pos1
print(pos1,pos2)
When this is run, where ob1 and ob2 are objects in the scene list “objs”, the terminal output is like so:
Blender Game Engine Started
<Vector (0.0000, -3.0000, 0.0000)> <Vector (10.0000, -3.0000, 0.0000)>
<Vector (10.0000, -3.0000, 0.0000)> <Vector (10.0000, -3.0000, 0.0000)>
Blender Game Engine Finished
notice how I printed the same variables, pos1 and pos2, at different times without changing them myself and yet it changed (here pos1 changed to the position of the second object, I suppose because it was first moved to the new location, so when I try to change the position of the second object it just stays where it is).
My question: what. And perhaps why. Maybe how? Yes, probably.
I know I could do a workaround where it gets the difference in xyz position and just increment each one by the (positive or negative) difference. Buuttt… this like fundamentally disrupts my knowledge of how python works. Or at least in the game engine. How is it that the variable is physically connected to the property of the object that is the xyz position?
Thanks for any help…
====
slight after thought, I tried the below (which worked), but here the value diff does not change as the objects themselves move (which is what I would expect). Even with the first code, posted above the '='s, if I set pos1 and pos2 to a and b or whatever, a and b still updated as pos1 and pos2 did.
def winSwap(ob1,ob2):
pos1 = objs[ob1].worldPosition
pos2 = objs[ob2].worldPosition
diff = pos1-pos2
print(diff)
objs[ob1].worldPosition-=diff
objs[ob2].worldPosition+=diff
print(diff)
(output)
<Vector (-10.0000, 0.0000, 0.0000)>
<Vector (-10.0000, 0.0000, 0.0000)>
notice here it stays the same.
Again, thanks for any help… I’d like to know if there are other such properties that will always ‘update’ or if there is some like proper way of programming to avoid variables changing implicitly. I’m sure this was a source of unknown error in the past for me.