Odd value behavior

I’ve run into a very odd scripting error. This is just a very basic script I’ve made to show it:

local original_angles = Angle(10,10,10)
local angles_backup = Angle(10,10,10)
local new_angles = original_angles
new_angles:RotateAroundAxis(new_angles:Forward(), 20)

MsgN("Backup: "..tostring(angles_backup))
MsgN("Original: "..tostring(original_angles))
MsgN("New: "..tostring(new_angles))

And the odd output of this is:

As you can see, RotateAroundAxis has changed the Original Angle as well, even though I called it on the New Angle

Can anyone explain or help me with this?

Angles are just a table of numbers and in lua tables tend to be passed by reference. So when you do new_angles = original_angles its just making new_angles reference original_angles. Use table.Copy if you need to create a copy of any table based data.

I guessed that too, because it would mostly happen when I did something like “new_angles.r = 50”.
I tried table.Copy but as expected, it only works on tables. Though I was able to use Angle:Set()

local new_angles = Angle()

Though, doesn’t it seem silly to have that extra work, just to make an angle have the same starting-value as another?

Also, isn’t this something fairly new in Gmod? I haven’t had problems with this before.

new_angles = original_angles * 1

Creates a new angle object and returns it - also works for vectors.

Ah thanks, that’s a lot easier :slight_smile: