Step by step:
- I assign self.target to equal the VALUE of another variable
- I change that value slightly
3 - 4. self.target was changed, but self.real_elbow_pos was ALSO changed??? What the heck?
Step by step:
“self.target = self.real_elbow_pos” means that you point it to the same object.
Same goes for the pos in “{pos = self.target}”
If you wish to make a copy: “self.target = vmath.vector3(self.real_elbow_pos)”
Thank you. Why does it work this way? Is there a benefit to doing that?
Complex types such as tables and vector3’s behave this way because of efficiency. Passing a huge table as argument to a function would otherwise require the runtime to do a copy of the table at each function call.
You can read more about lua tables here: Programming in Lua : 2.5
In particular:
You may think of a table as a dynamically allocated object; your program only manipulates references (or pointers) to them.
Vectors are userdata (right? @sicher): Programming in Lua : 2.7
It has no predefined operations in Lua, except assignment and equality test.
So, lua does not automatically create a copy in the assignment.
Yes.