Reset positions of objects

So, I wrote a thing that

  1. Goes through every object (for i in scene.objects) when the program starts, and .appends the world position of i onto a list if it has the game prop ‘reset’ (if ‘reset’ in i).
  2. Under certain circumstances (when I say so) it goes through every object again (for i in scene.objects) and sets its world position to the corresponding one in the list. (No objects are created/destroyed during runtime, so the order pretty straightforward)

However, I have a problem. The objects don’t move. When I print their world position, it’s where it should be - i.e moved - but the object, on screen, doesn’t move.

It gets even stranger - if I set the x,y, or z of any of the objects to be reset to some random value (float or int) they move just fine! Only when I get the value from the list do they not move.

What?

are you setting the value the second time again instead of the worldPosition?


list1=targets
list2=locations
x=0
for items in list1:
    items.worldPosition=list2[x]
    x=x+1

POI, you can use zip for this:


targets = ...
locations = ...

for target, location in zip(targets, locations):
    target.worldPosition = location

Someone has mentioned this before, but some important style conventions:

  • Don’t use items as a name for a loop variable. Things ended in “s” tend to be plural, and indicate a container (an iterable), whilst individual nouns refer to singular variables. Better to write “for item in items”, than “items in list1”.
  • Don’t use non-descriptive names for variables. Furthermore, avoid builtin names like list and tuple. (I.e don’t write list = … and definitely not list1 (the number doesn’t add any meaning).
  • Use inplace operators like += 1 rather than x = x + 1, it’s more pythonic.

@dragonspammer, this sounds like you want to implement a save/load system.

Be aware KX_GameObject.worldPosition returns a mathutils Vector, that refers to the object’s worldPosition attribute. It always reflects the current position at any time.
If you modify the stored object (e.g. you set the X-component), you directly modify the objects attribute -> you move the object around.

You do not understand what it means?
It means when you move an object, the vector in your list is referring to the new location of the object.

How to resolve this reference?
Store a different object, that refers to a position at the time of reading. Make a copy!


storedPosition = object.worldPosition.copy()

Worked!
Thanks monster!