I call a function twice with different parameters yet it doesn't change the outcome. The function always returns the same data.
[lua]
function scaleVertices(tblVertices, iScaleX, iScaleY)
--print(type(iScaleX) .. " " .. type(iScaleY))
for k, v in pairs(tblVertices) do
v.x = v.x * iScaleX
v.y = v.y * iScaleY
end
return tblVertices
end
local outerCircle = scaleVertices(tblVertices, iParametersTable.iScaleX, iParametersTable.iScaleY)
local innerCircle = scaleVertices(tblVertices, iParametersTable.iScaleX*iParametersTable.iArcWidth, iParametersTable.iScaleY*iParametersTable.iArcWidth)
innerCircle = table.Reverse(innerCircle) -- *
print("Printing outerCircle..")
PrintTable(outerCircle)
print("Printing innerCircle..")
PrintTable(innerCircle)
[/lua]
The printed innerCircle and outerCircle are identical. Yet if I made it print v.x and v.y after they'd been modified they were different. Any ideas?
That's because tables do not get automatically copied in Lua. Since you are passing tblVertices to the function, modifying it and then returning it, outerCircle, innerCircle and tblVertices reference the same table.
You can easily fix that by creating a copy of tblVertices in your scaleVertices function, working on that copy and then returning it.
To copy a table, use [url=http://wiki.garrysmod.com/page/table/Copy]table.Copy[/url].
Sorry, you need to Log In to post a reply to this thread.