Go.get_world_position doesn't give current pos if object has moved

Unlike the local pos from go.get_position(), the position reported by go.get_world_position() doesn’t update if the object’s position is changed.

Running the following:

local startp = go.get_position()
local wp1 = go.get_world_position()
local lp1 = vmath.vector3(startp)
lp1.x = lp1.x + 100
go.set_position(lp1)
local lp2 = go.get_position()
local wp2 = go.get_world_position()

print('\n', startp, wp1, '\n', lp2, wp2)

…gives you:

DEBUG:SCRIPT: 
	vmath.vector3(0, 0, 0)	vmath.vector3(0, 0, 0)	
	vmath.vector3(100, 0, 0)	vmath.vector3(0, 0, 0)

(The second world pos hasn’t updated.)

The world position is not immediately updated. You will be able to read the new world position after all transforms have been updated which I believe happens after the update() step of a frame. @Sven can probably tell you more.

2 Likes

Yeah, I understand that it’s a performance concern to update transforms extra times, but I would still expect it to be recalculated when I call go.get_world_position(), so I get the correct result.

Since it happens after update() (and after messages sent on update() it seems), is there any way of accessing it? The render script update happens after, but it doesn’t have access to go. functions, so that’s not helpful.

Not sure, posting a msg in update() maybe? @sven, any ideas?

As you point out; the reason it isn’t updated directly is to avoid making multiple transform calculations each time a gameobject position/rotation/scale is updated. Instead this is done in one swoop, over all the instances in the collection.

However, @britzl could be on to something, it might possible to send a message to the same script which should be handled directly after the update.

Some crappy test code here.

1 Like

Ah! Interesting. In my case I’m already doing some calculations “post-update” with this method, so I didn’t notice the difference. Thanks, this helps!

And I guess the transform is updated again afterward, since setting the object’s position in that message response works just fine. (this DefoldCodePad thing is very cool btw.)

Assuming this behavior won’t be changed anytime soon, I’ll make a suggestion on the doc page so other people don’t have to find it out the hard way.

1 Like

I just discovered this when trying to debug. It would help a ton to add this to the documentation for API reference (go)

1 Like

Good point! And done!

3 Likes

Great, thanks for the quick fix!