Getting location once

Hey,
So recently I’ve been facing a strange problem. For somehow when assigning an object’s world position to a variable once using this:

 if not "pos" in own: own["pos"] = own.worldPosition 

It keeps updating own[“pos”] every frame. though it doesn’t do so when I assign a vector instead like this:

 if not "pos" in own: own["pos"] = mathutils.Vector([0,0,0])

What’s happening wrong? is there any workaround to get the position only once eventually?

Thanks

worldPosition returns an object -> Vector. This object is referring to the position of the game object. This means if you move the game object, the position is changing. As you still refer to the position (no it’s values) the Vector seems to change to. But it just reflects the changes the game object was doing.

If you want to keep the position at the time of the operation, you have to create a new position. You can do that either by explicitly crate a new Vector


position = mathutils.Vector(gameObject.worldPosition)

or by using the copy operation


position = gameObject.worldPosition().copy()

To see more details on this topic, I suggest to search for “passing by reference” and “passing by value”.

Btw. Please do not place multiple statements at a single one line, especially no “if” statements. It makes the code awful hard to read.

Thanks, Monster!

This was just to keep the post clear, I don’t usually do it like that.