Storing dynamic variables as static

I spent about 20 minutes very confused with an issue where my variable was not staying set… I was using the following code:

GameLogic.globalDict["playerPositionInit"] = own.worldPosition

After some time, I realized it was not storing the values of own.worldPosition, it was storing the actual data, so when I printed it out again, it would not print my previous worldPosition, it would print my current one.

Is there an easy way to store the values as static? I need to do math on them, so can’t do something like print()… how do I accomplish this?

The overall thing I am trying to do is calculate how far the player has moved so I can update a minimap uvs. Thanks!

In Python, any time you assign a class object (like a KX_GameObject, Matrix, or in this case a Vector) to a variable, the variable actually contains what’s called a pointer to the object. A pointer is basically just a reference to where the object resides in memory. By doing this, programs save a ton of time and memory since they don’t have to duplicate an object every time you assign it to a new variable.

Since you haven’t made a copy of the object’s worldPosition Vector, the entry you’re saving in the global dictionary is just a pointer to the original object that is still being updated every frame. This means that if you want to keep a separate version of an object, you need to explicitly make a copy. The Vector class makes this easy since it has a handy copy() method.

GameLogic.globalDict["playerPositionInit"] = own.worldPosition.copy()

Alternatively, you could just save the object’s position as a list of primitives since Python doesn’t use pointers for those.

GameLogic.globalDict["playerPositionInit"] = [own.worldPosition.x, own.worldPosition.y, own.worldPosition.z]

You could try deepcopy too.

from copy import deepcopy
newList = deepcopy(oldList)

The copy function worked great. Thanks!