Script variable somehow affects gameobject in 2.5

Let’s say, i’ve got a script that runs in every frame and it’s owner - dynamic object.
I bring it above for both blender versions.

2.49 :


obj = GameLogic.getCurrentController().owner
pos = obj.position

pos[2] += 1
print (pos)

2.5 :


import bge
obj = bge.logic.getCurrentController().owner
pos = obj.position

pos[2] += 1
print (pos)

Script, defines a local variable ‘pos’ equals owner’s position, and increases it’s last coordinate.

The strange thing is, in 2.5 changing variable pos changes the object’s position.

Is it bug, feature or my incompetence?

And in last two cases, how to take some object’s parameter for further calculations without changing it?

  1. KX_GameObject.position is deprecated. Better use worldPosition or localPosition
  2. In 2.5x (*)position returns a reference to the position while 2.4x returns a copy of the position

That means in 2.5x if you change the values of pos, you set a new position of the gameObject.

Make an explicit copy to solve that:


pos = obj.position.copy()

BTW. in 2.5x (*)position returns a reference to a mathutils.Vector object, while 2.4x returns a copy of a list.

I hope it helps

Ah! great thank!
I watched many references about changes in new API, and didn’t noticed this one.