I have the next code and I'm trying to create a copy of the object Test:
local Test = {} do
local var1, var2 = {}
function Test:Init(val1, val2, val3)
var1.val1 = val1
var1.val2 = val2
var1.val3 = val3
end
function Test:Print()
print(var1.val1)
print(var1.val2)
print(var1.val3)
end
function Test:New(obj)
obj = obj or {}
setmetatable(obj, self)
self.__index = self
return obj
end
end
Then I'm creating my objects like this:
local obj1 = Test:New()
obj1:Init('a','b','c')
local obj2 = Test:New()
obj2:Init('c','d','e')
obj1:Print()
obj2:Print()
The result will be this:
c, d, e
c, d, e
As you can see the method New is not cloning correctly the object and is sharing the same instance. What am I doing wrong? I haven't cloned an object before and I'm having issues to understand how to do it.
I got that code from here: https://www.lua.org/pil/16.2.html
Thanks.