How does assigning to properties which aren't a basic type mechanically work?

Let’s assume I have a Node3D object. I can do this:

var p = object.position
p.x += 10

And the object won’t move, because the position is a value type. Vs I can do this:

object.position.x += 10

And it will move, presumably because I’m writing directly into the memory of the object?

But in that case how does the engine “know” that the position has been updated and that it potentially has to do work to update its state?

  • Is there some kind of hidden set_position call inserted?

  • If not, is the engine really reading every property of every node every frame just in case it changed?

  • Are there some cases where I have to “tell” the engine that I’ve updated the property to get it to actually update things?

  • Does it work that way for all non-basic type properties or is the transform special?

Hopefully you can tell what question I’m getting at here, thanks.

I might be misunderstanding your question as I am relatively new myself.

However if what you want is to update the x coordinate using the p variable, then you would have to reassign the value back to the object at some point using

object.position.x += p.x

as this updates the object value, whereas the variable p is not associated with the object at all.
type or paste code here

Every built-in property has a setter and getter.

When you set object.position.x += 10, you are calling object.set_position(value), which sends NOTIFICATION_LOCAL_TRANSFORM_CHANGED and updates.

When you do var p = object.position and p.x += 10 you are simply storing and manipulating a copy of object.position value.

I hope this helps!

1 Like

Okay, so for clarity when you write:

object.position.x += 10

It gets actually gets compiled something like this:

var temp = object.get_position()
object.set_position(Vector3(temp.x + 10, temp.y, temp.z))

Correct?

Yeah, that’s how setters and getters work. They are just methods with property-like syntax.